2 * GPIO based MDIO bitbang driver.
3 * Supports OpenFirmware.
5 * Copyright (c) 2008 CSE Semaphore Belgium.
6 * by Laurent Pinchart <laurentp@cse-semaphore.com>
8 * Copyright (C) 2008, Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
10 * Based on earlier work by
12 * Copyright (c) 2003 Intracom S.A.
13 * by Pantelis Antoniou <panto@intracom.gr>
15 * 2005 (c) MontaVista Software, Inc.
16 * Vitaly Bordug <vbordug@ru.mvista.com>
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.
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/interrupt.h>
26 #include <linux/platform_device.h>
27 #include <linux/gpio.h>
28 #include <linux/mdio-gpio.h>
30 #include <linux/of_gpio.h>
31 #include <linux/of_mdio.h>
33 struct mdio_gpio_info
{
34 struct mdiobb_ctrl ctrl
;
36 int mdc_active_low
, mdio_active_low
, mdo_active_low
;
39 static void *mdio_gpio_of_get_data(struct platform_device
*pdev
)
41 struct device_node
*np
= pdev
->dev
.of_node
;
42 struct mdio_gpio_platform_data
*pdata
;
43 enum of_gpio_flags flags
;
46 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
50 ret
= of_get_gpio_flags(np
, 0, &flags
);
55 pdata
->mdc_active_low
= flags
& OF_GPIO_ACTIVE_LOW
;
57 ret
= of_get_gpio_flags(np
, 1, &flags
);
61 pdata
->mdio_active_low
= flags
& OF_GPIO_ACTIVE_LOW
;
63 ret
= of_get_gpio_flags(np
, 2, &flags
);
66 pdata
->mdo_active_low
= flags
& OF_GPIO_ACTIVE_LOW
;
72 static void mdio_dir(struct mdiobb_ctrl
*ctrl
, int dir
)
74 struct mdio_gpio_info
*bitbang
=
75 container_of(ctrl
, struct mdio_gpio_info
, ctrl
);
78 /* Separate output pin. Always set its value to high
79 * when changing direction. If direction is input,
80 * assume the pin serves as pull-up. If direction is
81 * output, the default value is high.
83 gpio_set_value_cansleep(bitbang
->mdo
,
84 1 ^ bitbang
->mdo_active_low
);
89 gpio_direction_output(bitbang
->mdio
,
90 1 ^ bitbang
->mdio_active_low
);
92 gpio_direction_input(bitbang
->mdio
);
95 static int mdio_get(struct mdiobb_ctrl
*ctrl
)
97 struct mdio_gpio_info
*bitbang
=
98 container_of(ctrl
, struct mdio_gpio_info
, ctrl
);
100 return gpio_get_value_cansleep(bitbang
->mdio
) ^
101 bitbang
->mdio_active_low
;
104 static void mdio_set(struct mdiobb_ctrl
*ctrl
, int what
)
106 struct mdio_gpio_info
*bitbang
=
107 container_of(ctrl
, struct mdio_gpio_info
, ctrl
);
110 gpio_set_value_cansleep(bitbang
->mdo
,
111 what
^ bitbang
->mdo_active_low
);
113 gpio_set_value_cansleep(bitbang
->mdio
,
114 what
^ bitbang
->mdio_active_low
);
117 static void mdc_set(struct mdiobb_ctrl
*ctrl
, int what
)
119 struct mdio_gpio_info
*bitbang
=
120 container_of(ctrl
, struct mdio_gpio_info
, ctrl
);
122 gpio_set_value_cansleep(bitbang
->mdc
, what
^ bitbang
->mdc_active_low
);
125 static struct mdiobb_ops mdio_gpio_ops
= {
126 .owner
= THIS_MODULE
,
128 .set_mdio_dir
= mdio_dir
,
129 .set_mdio_data
= mdio_set
,
130 .get_mdio_data
= mdio_get
,
133 static struct mii_bus
*mdio_gpio_bus_init(struct device
*dev
,
134 struct mdio_gpio_platform_data
*pdata
,
137 struct mii_bus
*new_bus
;
138 struct mdio_gpio_info
*bitbang
;
141 bitbang
= devm_kzalloc(dev
, sizeof(*bitbang
), GFP_KERNEL
);
145 bitbang
->ctrl
.ops
= &mdio_gpio_ops
;
146 bitbang
->ctrl
.reset
= pdata
->reset
;
147 bitbang
->mdc
= pdata
->mdc
;
148 bitbang
->mdc_active_low
= pdata
->mdc_active_low
;
149 bitbang
->mdio
= pdata
->mdio
;
150 bitbang
->mdio_active_low
= pdata
->mdio_active_low
;
151 bitbang
->mdo
= pdata
->mdo
;
152 bitbang
->mdo_active_low
= pdata
->mdo_active_low
;
154 new_bus
= alloc_mdio_bitbang(&bitbang
->ctrl
);
158 new_bus
->name
= "GPIO Bitbanged MDIO",
160 new_bus
->phy_mask
= pdata
->phy_mask
;
161 new_bus
->phy_ignore_ta_mask
= pdata
->phy_ignore_ta_mask
;
162 new_bus
->irq
= pdata
->irqs
;
163 new_bus
->parent
= dev
;
165 if (new_bus
->phy_mask
== ~0)
168 for (i
= 0; i
< PHY_MAX_ADDR
; i
++)
169 if (!new_bus
->irq
[i
])
170 new_bus
->irq
[i
] = PHY_POLL
;
173 snprintf(new_bus
->id
, MII_BUS_ID_SIZE
, "gpio-%x", bus_id
);
175 strncpy(new_bus
->id
, "gpio", MII_BUS_ID_SIZE
);
177 if (devm_gpio_request(dev
, bitbang
->mdc
, "mdc"))
180 if (devm_gpio_request(dev
, bitbang
->mdio
, "mdio"))
184 if (devm_gpio_request(dev
, bitbang
->mdo
, "mdo"))
186 gpio_direction_output(bitbang
->mdo
, 1);
187 gpio_direction_input(bitbang
->mdio
);
190 gpio_direction_output(bitbang
->mdc
, 0);
192 dev_set_drvdata(dev
, new_bus
);
197 free_mdio_bitbang(new_bus
);
202 static void mdio_gpio_bus_deinit(struct device
*dev
)
204 struct mii_bus
*bus
= dev_get_drvdata(dev
);
206 free_mdio_bitbang(bus
);
209 static void mdio_gpio_bus_destroy(struct device
*dev
)
211 struct mii_bus
*bus
= dev_get_drvdata(dev
);
213 mdiobus_unregister(bus
);
214 mdio_gpio_bus_deinit(dev
);
217 static int mdio_gpio_probe(struct platform_device
*pdev
)
219 struct mdio_gpio_platform_data
*pdata
;
220 struct mii_bus
*new_bus
;
223 if (pdev
->dev
.of_node
) {
224 pdata
= mdio_gpio_of_get_data(pdev
);
225 bus_id
= of_alias_get_id(pdev
->dev
.of_node
, "mdio-gpio");
227 dev_warn(&pdev
->dev
, "failed to get alias id\n");
231 pdata
= dev_get_platdata(&pdev
->dev
);
238 new_bus
= mdio_gpio_bus_init(&pdev
->dev
, pdata
, bus_id
);
242 if (pdev
->dev
.of_node
)
243 ret
= of_mdiobus_register(new_bus
, pdev
->dev
.of_node
);
245 ret
= mdiobus_register(new_bus
);
248 mdio_gpio_bus_deinit(&pdev
->dev
);
253 static int mdio_gpio_remove(struct platform_device
*pdev
)
255 mdio_gpio_bus_destroy(&pdev
->dev
);
260 static const struct of_device_id mdio_gpio_of_match
[] = {
261 { .compatible
= "virtual,mdio-gpio", },
265 static struct platform_driver mdio_gpio_driver
= {
266 .probe
= mdio_gpio_probe
,
267 .remove
= mdio_gpio_remove
,
270 .of_match_table
= mdio_gpio_of_match
,
274 module_platform_driver(mdio_gpio_driver
);
276 MODULE_ALIAS("platform:mdio-gpio");
277 MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
278 MODULE_LICENSE("GPL");
279 MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");