xref: /openbmc/u-boot/drivers/phy/sandbox-phy.c (revision 3ce88cd7)
1 /*
2  * Copyright (C) 2017 Texas Instruments Incorporated - http://www.ti.com/
3  * Written by Jean-Jacques Hiblot  <jjhiblot@ti.com>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #include <common.h>
9 #include <dm.h>
10 #include <generic-phy.h>
11 
12 struct sandbox_phy_priv {
13 	bool initialized;
14 	bool on;
15 	bool broken;
16 };
17 
18 static int sandbox_phy_power_on(struct phy *phy)
19 {
20 	struct sandbox_phy_priv *priv = dev_get_priv(phy->dev);
21 
22 	if (!priv->initialized)
23 		return -EIO;
24 
25 	if (priv->broken)
26 		return -EIO;
27 
28 	priv->on = true;
29 
30 	return 0;
31 }
32 
33 static int sandbox_phy_power_off(struct phy *phy)
34 {
35 	struct sandbox_phy_priv *priv = dev_get_priv(phy->dev);
36 
37 	if (!priv->initialized)
38 		return -EIO;
39 
40 	if (priv->broken)
41 		return -EIO;
42 
43 	/*
44 	 * for validation purpose, let's says that power off
45 	 * works only for PHY 0
46 	 */
47 	if (phy->id)
48 		return -EIO;
49 
50 	priv->on = false;
51 
52 	return 0;
53 }
54 
55 static int sandbox_phy_init(struct phy *phy)
56 {
57 	struct sandbox_phy_priv *priv = dev_get_priv(phy->dev);
58 
59 	priv->initialized = true;
60 	priv->on = true;
61 
62 	return 0;
63 }
64 
65 static int sandbox_phy_exit(struct phy *phy)
66 {
67 	struct sandbox_phy_priv *priv = dev_get_priv(phy->dev);
68 
69 	priv->initialized = false;
70 	priv->on = false;
71 
72 	return 0;
73 }
74 
75 static int sandbox_phy_probe(struct udevice *dev)
76 {
77 	struct sandbox_phy_priv *priv = dev_get_priv(dev);
78 
79 	priv->initialized = false;
80 	priv->on = false;
81 	priv->broken = dev_read_bool(dev, "broken");
82 
83 	return 0;
84 }
85 
86 static struct phy_ops sandbox_phy_ops = {
87 	.power_on = sandbox_phy_power_on,
88 	.power_off = sandbox_phy_power_off,
89 	.init = sandbox_phy_init,
90 	.exit = sandbox_phy_exit,
91 };
92 
93 static const struct udevice_id sandbox_phy_ids[] = {
94 	{ .compatible = "sandbox,phy" },
95 	{ }
96 };
97 
98 U_BOOT_DRIVER(phy_sandbox) = {
99 	.name		= "phy_sandbox",
100 	.id		= UCLASS_PHY,
101 	.of_match	= sandbox_phy_ids,
102 	.ops		= &sandbox_phy_ops,
103 	.probe		= sandbox_phy_probe,
104 	.priv_auto_alloc_size = sizeof(struct sandbox_phy_priv),
105 };
106