• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * DaVinci MDIO Module driver
4  *
5  * Copyright (C) 2010 Texas Instruments.
6  *
7  * Shamelessly ripped out of davinci_emac.c, original copyrights follow:
8  *
9  * Copyright (C) 2009 Texas Instruments.
10  *
11  */
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/platform_device.h>
15 #include <linux/delay.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/phy.h>
19 #include <linux/clk.h>
20 #include <linux/err.h>
21 #include <linux/io.h>
22 #include <linux/iopoll.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/davinci_emac.h>
25 #include <linux/of.h>
26 #include <linux/of_device.h>
27 #include <linux/of_mdio.h>
28 #include <linux/pinctrl/consumer.h>
29 #include <linux/mdio-bitbang.h>
30 #include <linux/sys_soc.h>
31 
32 /*
33  * This timeout definition is a worst-case ultra defensive measure against
34  * unexpected controller lock ups.  Ideally, we should never ever hit this
35  * scenario in practice.
36  */
37 #define MDIO_TIMEOUT		100 /* msecs */
38 
39 #define PHY_REG_MASK		0x1f
40 #define PHY_ID_MASK		0x1f
41 
42 #define DEF_OUT_FREQ		2200000		/* 2.2 MHz */
43 
44 struct davinci_mdio_of_param {
45 	int autosuspend_delay_ms;
46 	bool manual_mode;
47 };
48 
49 struct davinci_mdio_regs {
50 	u32	version;
51 	u32	control;
52 #define CONTROL_IDLE		BIT(31)
53 #define CONTROL_ENABLE		BIT(30)
54 #define CONTROL_MAX_DIV		(0xffff)
55 #define CONTROL_CLKDIV		GENMASK(15, 0)
56 
57 #define MDIO_MAN_MDCLK_O	BIT(2)
58 #define MDIO_MAN_OE		BIT(1)
59 #define MDIO_MAN_PIN		BIT(0)
60 #define MDIO_MANUALMODE		BIT(31)
61 
62 #define MDIO_PIN               0
63 
64 
65 	u32	alive;
66 	u32	link;
67 	u32	linkintraw;
68 	u32	linkintmasked;
69 	u32	__reserved_0[2];
70 	u32	userintraw;
71 	u32	userintmasked;
72 	u32	userintmaskset;
73 	u32	userintmaskclr;
74 	u32	manualif;
75 	u32	poll;
76 	u32	__reserved_1[18];
77 
78 	struct {
79 		u32	access;
80 #define USERACCESS_GO		BIT(31)
81 #define USERACCESS_WRITE	BIT(30)
82 #define USERACCESS_ACK		BIT(29)
83 #define USERACCESS_READ		(0)
84 #define USERACCESS_DATA		(0xffff)
85 
86 		u32	physel;
87 	}	user[0];
88 };
89 
90 static const struct mdio_platform_data default_pdata = {
91 	.bus_freq = DEF_OUT_FREQ,
92 };
93 
94 struct davinci_mdio_data {
95 	struct mdio_platform_data pdata;
96 	struct mdiobb_ctrl bb_ctrl;
97 	struct davinci_mdio_regs __iomem *regs;
98 	struct clk	*clk;
99 	struct device	*dev;
100 	struct mii_bus	*bus;
101 	bool            active_in_suspend;
102 	unsigned long	access_time; /* jiffies */
103 	/* Indicates that driver shouldn't modify phy_mask in case
104 	 * if MDIO bus is registered from DT.
105 	 */
106 	bool		skip_scan;
107 	u32		clk_div;
108 	bool		manual_mode;
109 };
110 
davinci_mdio_init_clk(struct davinci_mdio_data * data)111 static void davinci_mdio_init_clk(struct davinci_mdio_data *data)
112 {
113 	u32 mdio_in, div, mdio_out_khz, access_time;
114 
115 	mdio_in = clk_get_rate(data->clk);
116 	div = (mdio_in / data->pdata.bus_freq) - 1;
117 	if (div > CONTROL_MAX_DIV)
118 		div = CONTROL_MAX_DIV;
119 
120 	data->clk_div = div;
121 	/*
122 	 * One mdio transaction consists of:
123 	 *	32 bits of preamble
124 	 *	32 bits of transferred data
125 	 *	24 bits of bus yield (not needed unless shared?)
126 	 */
127 	mdio_out_khz = mdio_in / (1000 * (div + 1));
128 	access_time  = (88 * 1000) / mdio_out_khz;
129 
130 	/*
131 	 * In the worst case, we could be kicking off a user-access immediately
132 	 * after the mdio bus scan state-machine triggered its own read.  If
133 	 * so, our request could get deferred by one access cycle.  We
134 	 * defensively allow for 4 access cycles.
135 	 */
136 	data->access_time = usecs_to_jiffies(access_time * 4);
137 	if (!data->access_time)
138 		data->access_time = 1;
139 }
140 
davinci_mdio_enable(struct davinci_mdio_data * data)141 static void davinci_mdio_enable(struct davinci_mdio_data *data)
142 {
143 	/* set enable and clock divider */
144 	writel(data->clk_div | CONTROL_ENABLE, &data->regs->control);
145 }
146 
davinci_mdio_disable(struct davinci_mdio_data * data)147 static void davinci_mdio_disable(struct davinci_mdio_data *data)
148 {
149 	u32 reg;
150 
151 	/* Disable MDIO state machine */
152 	reg = readl(&data->regs->control);
153 
154 	reg &= ~CONTROL_CLKDIV;
155 	reg |= data->clk_div;
156 
157 	reg &= ~CONTROL_ENABLE;
158 	writel(reg, &data->regs->control);
159 }
160 
davinci_mdio_enable_manual_mode(struct davinci_mdio_data * data)161 static void davinci_mdio_enable_manual_mode(struct davinci_mdio_data *data)
162 {
163 	u32 reg;
164 	/* set manual mode */
165 	reg = readl(&data->regs->poll);
166 	reg |= MDIO_MANUALMODE;
167 	writel(reg, &data->regs->poll);
168 }
169 
davinci_set_mdc(struct mdiobb_ctrl * ctrl,int level)170 static void davinci_set_mdc(struct mdiobb_ctrl *ctrl, int level)
171 {
172 	struct davinci_mdio_data *data;
173 	u32 reg;
174 
175 	data = container_of(ctrl, struct davinci_mdio_data, bb_ctrl);
176 	reg = readl(&data->regs->manualif);
177 
178 	if (level)
179 		reg |= MDIO_MAN_MDCLK_O;
180 	else
181 		reg &= ~MDIO_MAN_MDCLK_O;
182 
183 	writel(reg, &data->regs->manualif);
184 }
185 
davinci_set_mdio_dir(struct mdiobb_ctrl * ctrl,int output)186 static void davinci_set_mdio_dir(struct mdiobb_ctrl *ctrl, int output)
187 {
188 	struct davinci_mdio_data *data;
189 	u32 reg;
190 
191 	data = container_of(ctrl, struct davinci_mdio_data, bb_ctrl);
192 	reg = readl(&data->regs->manualif);
193 
194 	if (output)
195 		reg |= MDIO_MAN_OE;
196 	else
197 		reg &= ~MDIO_MAN_OE;
198 
199 	writel(reg, &data->regs->manualif);
200 }
201 
davinci_set_mdio_data(struct mdiobb_ctrl * ctrl,int value)202 static void  davinci_set_mdio_data(struct mdiobb_ctrl *ctrl, int value)
203 {
204 	struct davinci_mdio_data *data;
205 	u32 reg;
206 
207 	data = container_of(ctrl, struct davinci_mdio_data, bb_ctrl);
208 	reg = readl(&data->regs->manualif);
209 
210 	if (value)
211 		reg |= MDIO_MAN_PIN;
212 	else
213 		reg &= ~MDIO_MAN_PIN;
214 
215 	writel(reg, &data->regs->manualif);
216 }
217 
davinci_get_mdio_data(struct mdiobb_ctrl * ctrl)218 static int davinci_get_mdio_data(struct mdiobb_ctrl *ctrl)
219 {
220 	struct davinci_mdio_data *data;
221 	unsigned long reg;
222 
223 	data = container_of(ctrl, struct davinci_mdio_data, bb_ctrl);
224 	reg = readl(&data->regs->manualif);
225 	return test_bit(MDIO_PIN, &reg);
226 }
227 
davinci_mdiobb_read(struct mii_bus * bus,int phy,int reg)228 static int davinci_mdiobb_read(struct mii_bus *bus, int phy, int reg)
229 {
230 	int ret;
231 
232 	ret = pm_runtime_resume_and_get(bus->parent);
233 	if (ret < 0)
234 		return ret;
235 
236 	ret = mdiobb_read(bus, phy, reg);
237 
238 	pm_runtime_mark_last_busy(bus->parent);
239 	pm_runtime_put_autosuspend(bus->parent);
240 
241 	return ret;
242 }
243 
davinci_mdiobb_write(struct mii_bus * bus,int phy,int reg,u16 val)244 static int davinci_mdiobb_write(struct mii_bus *bus, int phy, int reg,
245 				u16 val)
246 {
247 	int ret;
248 
249 	ret = pm_runtime_resume_and_get(bus->parent);
250 	if (ret < 0)
251 		return ret;
252 
253 	ret = mdiobb_write(bus, phy, reg, val);
254 
255 	pm_runtime_mark_last_busy(bus->parent);
256 	pm_runtime_put_autosuspend(bus->parent);
257 
258 	return ret;
259 }
260 
davinci_mdio_common_reset(struct davinci_mdio_data * data)261 static int davinci_mdio_common_reset(struct davinci_mdio_data *data)
262 {
263 	u32 phy_mask, ver;
264 	int ret;
265 
266 	ret = pm_runtime_get_sync(data->dev);
267 	if (ret < 0) {
268 		pm_runtime_put_noidle(data->dev);
269 		return ret;
270 	}
271 
272 	if (data->manual_mode) {
273 		davinci_mdio_disable(data);
274 		davinci_mdio_enable_manual_mode(data);
275 	}
276 
277 	/* wait for scan logic to settle */
278 	msleep(PHY_MAX_ADDR * data->access_time);
279 
280 	/* dump hardware version info */
281 	ver = readl(&data->regs->version);
282 	dev_info(data->dev,
283 		 "davinci mdio revision %d.%d, bus freq %ld\n",
284 		 (ver >> 8) & 0xff, ver & 0xff,
285 		 data->pdata.bus_freq);
286 
287 	if (data->skip_scan)
288 		goto done;
289 
290 	/* get phy mask from the alive register */
291 	phy_mask = readl(&data->regs->alive);
292 	if (phy_mask) {
293 		/* restrict mdio bus to live phys only */
294 		dev_info(data->dev, "detected phy mask %x\n", ~phy_mask);
295 		phy_mask = ~phy_mask;
296 	} else {
297 		/* desperately scan all phys */
298 		dev_warn(data->dev, "no live phy, scanning all\n");
299 		phy_mask = 0;
300 	}
301 	data->bus->phy_mask = phy_mask;
302 
303 done:
304 	pm_runtime_mark_last_busy(data->dev);
305 	pm_runtime_put_autosuspend(data->dev);
306 
307 	return 0;
308 }
309 
davinci_mdio_reset(struct mii_bus * bus)310 static int davinci_mdio_reset(struct mii_bus *bus)
311 {
312 	struct davinci_mdio_data *data = bus->priv;
313 
314 	return davinci_mdio_common_reset(data);
315 }
316 
davinci_mdiobb_reset(struct mii_bus * bus)317 static int davinci_mdiobb_reset(struct mii_bus *bus)
318 {
319 	struct mdiobb_ctrl *ctrl = bus->priv;
320 	struct davinci_mdio_data *data;
321 
322 	data = container_of(ctrl, struct davinci_mdio_data, bb_ctrl);
323 
324 	return davinci_mdio_common_reset(data);
325 }
326 
327 /* wait until hardware is ready for another user access */
wait_for_user_access(struct davinci_mdio_data * data)328 static inline int wait_for_user_access(struct davinci_mdio_data *data)
329 {
330 	struct davinci_mdio_regs __iomem *regs = data->regs;
331 	unsigned long timeout = jiffies + msecs_to_jiffies(MDIO_TIMEOUT);
332 	u32 reg;
333 
334 	while (time_after(timeout, jiffies)) {
335 		reg = readl(&regs->user[0].access);
336 		if ((reg & USERACCESS_GO) == 0)
337 			return 0;
338 
339 		reg = readl(&regs->control);
340 		if ((reg & CONTROL_IDLE) == 0) {
341 			usleep_range(100, 200);
342 			continue;
343 		}
344 
345 		/*
346 		 * An emac soft_reset may have clobbered the mdio controller's
347 		 * state machine.  We need to reset and retry the current
348 		 * operation
349 		 */
350 		dev_warn(data->dev, "resetting idled controller\n");
351 		davinci_mdio_enable(data);
352 		return -EAGAIN;
353 	}
354 
355 	reg = readl(&regs->user[0].access);
356 	if ((reg & USERACCESS_GO) == 0)
357 		return 0;
358 
359 	dev_err(data->dev, "timed out waiting for user access\n");
360 	return -ETIMEDOUT;
361 }
362 
363 /* wait until hardware state machine is idle */
wait_for_idle(struct davinci_mdio_data * data)364 static inline int wait_for_idle(struct davinci_mdio_data *data)
365 {
366 	struct davinci_mdio_regs __iomem *regs = data->regs;
367 	u32 val, ret;
368 
369 	ret = readl_poll_timeout(&regs->control, val, val & CONTROL_IDLE,
370 				 0, MDIO_TIMEOUT * 1000);
371 	if (ret)
372 		dev_err(data->dev, "timed out waiting for idle\n");
373 
374 	return ret;
375 }
376 
davinci_mdio_read(struct mii_bus * bus,int phy_id,int phy_reg)377 static int davinci_mdio_read(struct mii_bus *bus, int phy_id, int phy_reg)
378 {
379 	struct davinci_mdio_data *data = bus->priv;
380 	u32 reg;
381 	int ret;
382 
383 	if (phy_reg & ~PHY_REG_MASK || phy_id & ~PHY_ID_MASK)
384 		return -EINVAL;
385 
386 	ret = pm_runtime_get_sync(data->dev);
387 	if (ret < 0) {
388 		pm_runtime_put_noidle(data->dev);
389 		return ret;
390 	}
391 
392 	reg = (USERACCESS_GO | USERACCESS_READ | (phy_reg << 21) |
393 	       (phy_id << 16));
394 
395 	while (1) {
396 		ret = wait_for_user_access(data);
397 		if (ret == -EAGAIN)
398 			continue;
399 		if (ret < 0)
400 			break;
401 
402 		writel(reg, &data->regs->user[0].access);
403 
404 		ret = wait_for_user_access(data);
405 		if (ret == -EAGAIN)
406 			continue;
407 		if (ret < 0)
408 			break;
409 
410 		reg = readl(&data->regs->user[0].access);
411 		ret = (reg & USERACCESS_ACK) ? (reg & USERACCESS_DATA) : -EIO;
412 		break;
413 	}
414 
415 	pm_runtime_mark_last_busy(data->dev);
416 	pm_runtime_put_autosuspend(data->dev);
417 	return ret;
418 }
419 
davinci_mdio_write(struct mii_bus * bus,int phy_id,int phy_reg,u16 phy_data)420 static int davinci_mdio_write(struct mii_bus *bus, int phy_id,
421 			      int phy_reg, u16 phy_data)
422 {
423 	struct davinci_mdio_data *data = bus->priv;
424 	u32 reg;
425 	int ret;
426 
427 	if (phy_reg & ~PHY_REG_MASK || phy_id & ~PHY_ID_MASK)
428 		return -EINVAL;
429 
430 	ret = pm_runtime_get_sync(data->dev);
431 	if (ret < 0) {
432 		pm_runtime_put_noidle(data->dev);
433 		return ret;
434 	}
435 
436 	reg = (USERACCESS_GO | USERACCESS_WRITE | (phy_reg << 21) |
437 		   (phy_id << 16) | (phy_data & USERACCESS_DATA));
438 
439 	while (1) {
440 		ret = wait_for_user_access(data);
441 		if (ret == -EAGAIN)
442 			continue;
443 		if (ret < 0)
444 			break;
445 
446 		writel(reg, &data->regs->user[0].access);
447 
448 		ret = wait_for_user_access(data);
449 		if (ret == -EAGAIN)
450 			continue;
451 		break;
452 	}
453 
454 	pm_runtime_mark_last_busy(data->dev);
455 	pm_runtime_put_autosuspend(data->dev);
456 
457 	return ret;
458 }
459 
davinci_mdio_probe_dt(struct mdio_platform_data * data,struct platform_device * pdev)460 static int davinci_mdio_probe_dt(struct mdio_platform_data *data,
461 			 struct platform_device *pdev)
462 {
463 	struct device_node *node = pdev->dev.of_node;
464 	u32 prop;
465 
466 	if (!node)
467 		return -EINVAL;
468 
469 	if (of_property_read_u32(node, "bus_freq", &prop)) {
470 		dev_err(&pdev->dev, "Missing bus_freq property in the DT.\n");
471 		return -EINVAL;
472 	}
473 	data->bus_freq = prop;
474 
475 	return 0;
476 }
477 
478 struct k3_mdio_soc_data {
479 	bool manual_mode;
480 };
481 
482 static const struct k3_mdio_soc_data am65_mdio_soc_data = {
483 	.manual_mode = true,
484 };
485 
486 static const struct soc_device_attribute k3_mdio_socinfo[] = {
487 	{ .family = "AM62X", .revision = "SR1.0", .data = &am65_mdio_soc_data },
488 	{ .family = "AM64X", .revision = "SR1.0", .data = &am65_mdio_soc_data },
489 	{ .family = "AM64X", .revision = "SR2.0", .data = &am65_mdio_soc_data },
490 	{ .family = "AM65X", .revision = "SR1.0", .data = &am65_mdio_soc_data },
491 	{ .family = "AM65X", .revision = "SR2.0", .data = &am65_mdio_soc_data },
492 	{ .family = "J7200", .revision = "SR1.0", .data = &am65_mdio_soc_data },
493 	{ .family = "J7200", .revision = "SR2.0", .data = &am65_mdio_soc_data },
494 	{ .family = "J721E", .revision = "SR1.0", .data = &am65_mdio_soc_data },
495 	{ .family = "J721E", .revision = "SR2.0", .data = &am65_mdio_soc_data },
496 	{ .family = "J721S2", .revision = "SR1.0", .data = &am65_mdio_soc_data},
497 	{ /* sentinel */ },
498 };
499 
500 #if IS_ENABLED(CONFIG_OF)
501 static const struct davinci_mdio_of_param of_cpsw_mdio_data = {
502 	.autosuspend_delay_ms = 100,
503 };
504 
505 static const struct of_device_id davinci_mdio_of_mtable[] = {
506 	{ .compatible = "ti,davinci_mdio", },
507 	{ .compatible = "ti,cpsw-mdio", .data = &of_cpsw_mdio_data},
508 	{ /* sentinel */ },
509 };
510 MODULE_DEVICE_TABLE(of, davinci_mdio_of_mtable);
511 #endif
512 
513 static const struct mdiobb_ops davinci_mdiobb_ops = {
514 	.owner = THIS_MODULE,
515 	.set_mdc = davinci_set_mdc,
516 	.set_mdio_dir = davinci_set_mdio_dir,
517 	.set_mdio_data = davinci_set_mdio_data,
518 	.get_mdio_data = davinci_get_mdio_data,
519 };
520 
davinci_mdio_probe(struct platform_device * pdev)521 static int davinci_mdio_probe(struct platform_device *pdev)
522 {
523 	struct mdio_platform_data *pdata = dev_get_platdata(&pdev->dev);
524 	struct device *dev = &pdev->dev;
525 	struct davinci_mdio_data *data;
526 	struct resource *res;
527 	struct phy_device *phy;
528 	int ret, addr;
529 	int autosuspend_delay_ms = -1;
530 
531 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
532 	if (!data)
533 		return -ENOMEM;
534 
535 	data->manual_mode = false;
536 	data->bb_ctrl.ops = &davinci_mdiobb_ops;
537 
538 	if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
539 		const struct soc_device_attribute *soc_match_data;
540 
541 		soc_match_data = soc_device_match(k3_mdio_socinfo);
542 		if (soc_match_data && soc_match_data->data) {
543 			const struct k3_mdio_soc_data *socdata =
544 						soc_match_data->data;
545 
546 			data->manual_mode = socdata->manual_mode;
547 		}
548 	}
549 
550 	if (data->manual_mode)
551 		data->bus = alloc_mdio_bitbang(&data->bb_ctrl);
552 	else
553 		data->bus = devm_mdiobus_alloc(dev);
554 
555 	if (!data->bus) {
556 		dev_err(dev, "failed to alloc mii bus\n");
557 		return -ENOMEM;
558 	}
559 
560 	if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
561 		const struct davinci_mdio_of_param *of_mdio_data;
562 
563 		ret = davinci_mdio_probe_dt(&data->pdata, pdev);
564 		if (ret)
565 			return ret;
566 		snprintf(data->bus->id, MII_BUS_ID_SIZE, "%s", pdev->name);
567 
568 		of_mdio_data = of_device_get_match_data(&pdev->dev);
569 		if (of_mdio_data) {
570 			autosuspend_delay_ms =
571 					of_mdio_data->autosuspend_delay_ms;
572 		}
573 	} else {
574 		data->pdata = pdata ? (*pdata) : default_pdata;
575 		snprintf(data->bus->id, MII_BUS_ID_SIZE, "%s-%x",
576 			 pdev->name, pdev->id);
577 	}
578 
579 	data->bus->name		= dev_name(dev);
580 
581 	if (data->manual_mode) {
582 		data->bus->read		= davinci_mdiobb_read;
583 		data->bus->write	= davinci_mdiobb_write;
584 		data->bus->reset	= davinci_mdiobb_reset;
585 
586 		dev_info(dev, "Configuring MDIO in manual mode\n");
587 	} else {
588 		data->bus->read		= davinci_mdio_read;
589 		data->bus->write	= davinci_mdio_write;
590 		data->bus->reset	= davinci_mdio_reset;
591 		data->bus->priv		= data;
592 	}
593 	data->bus->parent	= dev;
594 
595 	data->clk = devm_clk_get(dev, "fck");
596 	if (IS_ERR(data->clk)) {
597 		dev_err(dev, "failed to get device clock\n");
598 		return PTR_ERR(data->clk);
599 	}
600 
601 	dev_set_drvdata(dev, data);
602 	data->dev = dev;
603 
604 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
605 	if (!res)
606 		return -EINVAL;
607 	data->regs = devm_ioremap(dev, res->start, resource_size(res));
608 	if (!data->regs)
609 		return -ENOMEM;
610 
611 	davinci_mdio_init_clk(data);
612 
613 	pm_runtime_set_autosuspend_delay(&pdev->dev, autosuspend_delay_ms);
614 	pm_runtime_use_autosuspend(&pdev->dev);
615 	pm_runtime_enable(&pdev->dev);
616 
617 	/* register the mii bus
618 	 * Create PHYs from DT only in case if PHY child nodes are explicitly
619 	 * defined to support backward compatibility with DTs which assume that
620 	 * Davinci MDIO will always scan the bus for PHYs detection.
621 	 */
622 	if (dev->of_node && of_get_child_count(dev->of_node))
623 		data->skip_scan = true;
624 
625 	ret = of_mdiobus_register(data->bus, dev->of_node);
626 	if (ret)
627 		goto bail_out;
628 
629 	/* scan and dump the bus */
630 	for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
631 		phy = mdiobus_get_phy(data->bus, addr);
632 		if (phy) {
633 			dev_info(dev, "phy[%d]: device %s, driver %s\n",
634 				 phy->mdio.addr, phydev_name(phy),
635 				 phy->drv ? phy->drv->name : "unknown");
636 		}
637 	}
638 
639 	return 0;
640 
641 bail_out:
642 	pm_runtime_dont_use_autosuspend(&pdev->dev);
643 	pm_runtime_disable(&pdev->dev);
644 	return ret;
645 }
646 
davinci_mdio_remove(struct platform_device * pdev)647 static int davinci_mdio_remove(struct platform_device *pdev)
648 {
649 	struct davinci_mdio_data *data = platform_get_drvdata(pdev);
650 
651 	if (data->bus) {
652 		mdiobus_unregister(data->bus);
653 
654 		if (data->manual_mode)
655 			free_mdio_bitbang(data->bus);
656 	}
657 
658 	pm_runtime_dont_use_autosuspend(&pdev->dev);
659 	pm_runtime_disable(&pdev->dev);
660 
661 	return 0;
662 }
663 
664 #ifdef CONFIG_PM
davinci_mdio_runtime_suspend(struct device * dev)665 static int davinci_mdio_runtime_suspend(struct device *dev)
666 {
667 	struct davinci_mdio_data *data = dev_get_drvdata(dev);
668 	u32 ctrl;
669 
670 	/* shutdown the scan state machine */
671 	ctrl = readl(&data->regs->control);
672 	ctrl &= ~CONTROL_ENABLE;
673 	writel(ctrl, &data->regs->control);
674 
675 	if (!data->manual_mode)
676 		wait_for_idle(data);
677 
678 	return 0;
679 }
680 
davinci_mdio_runtime_resume(struct device * dev)681 static int davinci_mdio_runtime_resume(struct device *dev)
682 {
683 	struct davinci_mdio_data *data = dev_get_drvdata(dev);
684 
685 	if (data->manual_mode) {
686 		davinci_mdio_disable(data);
687 		davinci_mdio_enable_manual_mode(data);
688 	} else {
689 		davinci_mdio_enable(data);
690 	}
691 	return 0;
692 }
693 #endif
694 
695 #ifdef CONFIG_PM_SLEEP
davinci_mdio_suspend(struct device * dev)696 static int davinci_mdio_suspend(struct device *dev)
697 {
698 	struct davinci_mdio_data *data = dev_get_drvdata(dev);
699 	int ret = 0;
700 
701 	data->active_in_suspend = !pm_runtime_status_suspended(dev);
702 	if (data->active_in_suspend)
703 		ret = pm_runtime_force_suspend(dev);
704 	if (ret < 0)
705 		return ret;
706 
707 	/* Select sleep pin state */
708 	pinctrl_pm_select_sleep_state(dev);
709 
710 	return 0;
711 }
712 
davinci_mdio_resume(struct device * dev)713 static int davinci_mdio_resume(struct device *dev)
714 {
715 	struct davinci_mdio_data *data = dev_get_drvdata(dev);
716 
717 	/* Select default pin state */
718 	pinctrl_pm_select_default_state(dev);
719 
720 	if (data->active_in_suspend)
721 		pm_runtime_force_resume(dev);
722 
723 	return 0;
724 }
725 #endif
726 
727 static const struct dev_pm_ops davinci_mdio_pm_ops = {
728 	SET_RUNTIME_PM_OPS(davinci_mdio_runtime_suspend,
729 			   davinci_mdio_runtime_resume, NULL)
730 	SET_LATE_SYSTEM_SLEEP_PM_OPS(davinci_mdio_suspend, davinci_mdio_resume)
731 };
732 
733 static struct platform_driver davinci_mdio_driver = {
734 	.driver = {
735 		.name	 = "davinci_mdio",
736 		.pm	 = &davinci_mdio_pm_ops,
737 		.of_match_table = of_match_ptr(davinci_mdio_of_mtable),
738 	},
739 	.probe = davinci_mdio_probe,
740 	.remove = davinci_mdio_remove,
741 };
742 
davinci_mdio_init(void)743 static int __init davinci_mdio_init(void)
744 {
745 	return platform_driver_register(&davinci_mdio_driver);
746 }
747 device_initcall(davinci_mdio_init);
748 
davinci_mdio_exit(void)749 static void __exit davinci_mdio_exit(void)
750 {
751 	platform_driver_unregister(&davinci_mdio_driver);
752 }
753 module_exit(davinci_mdio_exit);
754 
755 MODULE_LICENSE("GPL");
756 MODULE_DESCRIPTION("DaVinci MDIO driver");
757