24
24
#include <linux/slab.h>
25
25
#include <linux/interrupt.h>
26
26
#include <linux/platform_device.h>
27
+ #include <linux/mdio-bitbang.h>
28
+ #include <linux/mdio-gpio.h>
27
29
#include <linux/gpio.h>
28
- #include <linux/platform_data/mdio- gpio.h>
30
+ #include <linux/gpio/consumer .h>
29
31
30
32
#include <linux/of_gpio.h>
31
33
#include <linux/of_mdio.h>
@@ -35,37 +37,22 @@ struct mdio_gpio_info {
35
37
struct gpio_desc * mdc , * mdio , * mdo ;
36
38
};
37
39
38
- static void * mdio_gpio_of_get_data (struct platform_device * pdev )
40
+ static int mdio_gpio_get_data (struct device * dev ,
41
+ struct mdio_gpio_info * bitbang )
39
42
{
40
- struct device_node * np = pdev -> dev .of_node ;
41
- struct mdio_gpio_platform_data * pdata ;
42
- enum of_gpio_flags flags ;
43
- int ret ;
44
-
45
- pdata = devm_kzalloc (& pdev -> dev , sizeof (* pdata ), GFP_KERNEL );
46
- if (!pdata )
47
- return NULL ;
48
-
49
- ret = of_get_gpio_flags (np , 0 , & flags );
50
- if (ret < 0 )
51
- return NULL ;
52
-
53
- pdata -> mdc = ret ;
54
- pdata -> mdc_active_low = flags & OF_GPIO_ACTIVE_LOW ;
55
-
56
- ret = of_get_gpio_flags (np , 1 , & flags );
57
- if (ret < 0 )
58
- return NULL ;
59
- pdata -> mdio = ret ;
60
- pdata -> mdio_active_low = flags & OF_GPIO_ACTIVE_LOW ;
61
-
62
- ret = of_get_gpio_flags (np , 2 , & flags );
63
- if (ret > 0 ) {
64
- pdata -> mdo = ret ;
65
- pdata -> mdo_active_low = flags & OF_GPIO_ACTIVE_LOW ;
66
- }
67
-
68
- return pdata ;
43
+ bitbang -> mdc = devm_gpiod_get_index (dev , NULL , MDIO_GPIO_MDC ,
44
+ GPIOD_OUT_LOW );
45
+ if (IS_ERR (bitbang -> mdc ))
46
+ return PTR_ERR (bitbang -> mdc );
47
+
48
+ bitbang -> mdio = devm_gpiod_get_index (dev , NULL , MDIO_GPIO_MDIO ,
49
+ GPIOD_IN );
50
+ if (IS_ERR (bitbang -> mdio ))
51
+ return PTR_ERR (bitbang -> mdio );
52
+
53
+ bitbang -> mdo = devm_gpiod_get_index_optional (dev , NULL , MDIO_GPIO_MDO ,
54
+ GPIOD_OUT_LOW );
55
+ return PTR_ERR_OR_ZERO (bitbang -> mdo );
69
56
}
70
57
71
58
static void mdio_dir (struct mdiobb_ctrl * ctrl , int dir )
@@ -125,78 +112,28 @@ static const struct mdiobb_ops mdio_gpio_ops = {
125
112
};
126
113
127
114
static struct mii_bus * mdio_gpio_bus_init (struct device * dev ,
128
- struct mdio_gpio_platform_data * pdata ,
115
+ struct mdio_gpio_info * bitbang ,
129
116
int bus_id )
130
117
{
131
118
struct mii_bus * new_bus ;
132
- struct mdio_gpio_info * bitbang ;
133
- int i ;
134
- int mdc , mdio , mdo ;
135
- unsigned long mdc_flags = GPIOF_OUT_INIT_LOW ;
136
- unsigned long mdio_flags = GPIOF_DIR_IN ;
137
- unsigned long mdo_flags = GPIOF_OUT_INIT_HIGH ;
138
-
139
- bitbang = devm_kzalloc (dev , sizeof (* bitbang ), GFP_KERNEL );
140
- if (!bitbang )
141
- goto out ;
142
119
143
120
bitbang -> ctrl .ops = & mdio_gpio_ops ;
144
- bitbang -> ctrl .reset = pdata -> reset ;
145
- mdc = pdata -> mdc ;
146
- bitbang -> mdc = gpio_to_desc (mdc );
147
- if (pdata -> mdc_active_low )
148
- mdc_flags = GPIOF_OUT_INIT_HIGH | GPIOF_ACTIVE_LOW ;
149
- mdio = pdata -> mdio ;
150
- bitbang -> mdio = gpio_to_desc (mdio );
151
- if (pdata -> mdio_active_low )
152
- mdio_flags |= GPIOF_ACTIVE_LOW ;
153
- mdo = pdata -> mdo ;
154
- if (mdo ) {
155
- bitbang -> mdo = gpio_to_desc (mdo );
156
- if (pdata -> mdo_active_low )
157
- mdo_flags = GPIOF_OUT_INIT_LOW | GPIOF_ACTIVE_LOW ;
158
- }
159
121
160
122
new_bus = alloc_mdio_bitbang (& bitbang -> ctrl );
161
123
if (!new_bus )
162
- goto out ;
163
-
164
- new_bus -> name = "GPIO Bitbanged MDIO" ,
124
+ return NULL ;
165
125
166
- new_bus -> phy_mask = pdata -> phy_mask ;
167
- new_bus -> phy_ignore_ta_mask = pdata -> phy_ignore_ta_mask ;
168
- memcpy (new_bus -> irq , pdata -> irqs , sizeof (new_bus -> irq ));
126
+ new_bus -> name = "GPIO Bitbanged MDIO" ;
169
127
new_bus -> parent = dev ;
170
128
171
- if (new_bus -> phy_mask == ~0 )
172
- goto out_free_bus ;
173
-
174
- for (i = 0 ; i < PHY_MAX_ADDR ; i ++ )
175
- if (!new_bus -> irq [i ])
176
- new_bus -> irq [i ] = PHY_POLL ;
177
-
178
129
if (bus_id != -1 )
179
130
snprintf (new_bus -> id , MII_BUS_ID_SIZE , "gpio-%x" , bus_id );
180
131
else
181
132
strncpy (new_bus -> id , "gpio" , MII_BUS_ID_SIZE );
182
133
183
- if (devm_gpio_request_one (dev , mdc , mdc_flags , "mdc" ))
184
- goto out_free_bus ;
185
-
186
- if (devm_gpio_request_one (dev , mdio , mdio_flags , "mdio" ))
187
- goto out_free_bus ;
188
-
189
- if (mdo && devm_gpio_request_one (dev , mdo , mdo_flags , "mdo" ))
190
- goto out_free_bus ;
191
-
192
134
dev_set_drvdata (dev , new_bus );
193
135
194
136
return new_bus ;
195
-
196
- out_free_bus :
197
- free_mdio_bitbang (new_bus );
198
- out :
199
- return NULL ;
200
137
}
201
138
202
139
static void mdio_gpio_bus_deinit (struct device * dev )
@@ -216,26 +153,29 @@ static void mdio_gpio_bus_destroy(struct device *dev)
216
153
217
154
static int mdio_gpio_probe (struct platform_device * pdev )
218
155
{
219
- struct mdio_gpio_platform_data * pdata ;
156
+ struct mdio_gpio_info * bitbang ;
220
157
struct mii_bus * new_bus ;
221
158
int ret , bus_id ;
222
159
160
+ bitbang = devm_kzalloc (& pdev -> dev , sizeof (* bitbang ), GFP_KERNEL );
161
+ if (!bitbang )
162
+ return - ENOMEM ;
163
+
164
+ ret = mdio_gpio_get_data (& pdev -> dev , bitbang );
165
+ if (ret )
166
+ return ret ;
167
+
223
168
if (pdev -> dev .of_node ) {
224
- pdata = mdio_gpio_of_get_data (pdev );
225
169
bus_id = of_alias_get_id (pdev -> dev .of_node , "mdio-gpio" );
226
170
if (bus_id < 0 ) {
227
171
dev_warn (& pdev -> dev , "failed to get alias id\n" );
228
172
bus_id = 0 ;
229
173
}
230
174
} else {
231
- pdata = dev_get_platdata (& pdev -> dev );
232
175
bus_id = pdev -> id ;
233
176
}
234
177
235
- if (!pdata )
236
- return - ENODEV ;
237
-
238
- new_bus = mdio_gpio_bus_init (& pdev -> dev , pdata , bus_id );
178
+ new_bus = mdio_gpio_bus_init (& pdev -> dev , bitbang , bus_id );
239
179
if (!new_bus )
240
180
return - ENODEV ;
241
181
0 commit comments