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