Jean-Jacques Hiblot | 86322f5 | 2017-04-24 11:51:28 +0200 | [diff] [blame] | 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 | DECLARE_GLOBAL_DATA_PTR; |
| 13 | |
| 14 | struct sandbox_phy_priv { |
| 15 | bool initialized; |
| 16 | bool on; |
| 17 | bool broken; |
| 18 | }; |
| 19 | |
| 20 | static int sandbox_phy_power_on(struct phy *phy) |
| 21 | { |
| 22 | struct sandbox_phy_priv *priv = dev_get_priv(phy->dev); |
| 23 | |
| 24 | if (!priv->initialized) |
| 25 | return -EIO; |
| 26 | |
| 27 | if (priv->broken) |
| 28 | return -EIO; |
| 29 | |
| 30 | priv->on = true; |
| 31 | |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | static int sandbox_phy_power_off(struct phy *phy) |
| 36 | { |
| 37 | struct sandbox_phy_priv *priv = dev_get_priv(phy->dev); |
| 38 | |
| 39 | if (!priv->initialized) |
| 40 | return -EIO; |
| 41 | |
| 42 | if (priv->broken) |
| 43 | return -EIO; |
| 44 | |
| 45 | /* |
| 46 | * for validation purpose, let's says that power off |
| 47 | * works only for PHY 0 |
| 48 | */ |
| 49 | if (phy->id) |
| 50 | return -EIO; |
| 51 | |
| 52 | priv->on = false; |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | static int sandbox_phy_init(struct phy *phy) |
| 58 | { |
| 59 | struct sandbox_phy_priv *priv = dev_get_priv(phy->dev); |
| 60 | |
| 61 | priv->initialized = true; |
| 62 | priv->on = true; |
| 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | static int sandbox_phy_exit(struct phy *phy) |
| 68 | { |
| 69 | struct sandbox_phy_priv *priv = dev_get_priv(phy->dev); |
| 70 | |
| 71 | priv->initialized = false; |
| 72 | priv->on = false; |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | static int sandbox_phy_probe(struct udevice *dev) |
| 78 | { |
| 79 | struct sandbox_phy_priv *priv = dev_get_priv(dev); |
| 80 | |
| 81 | priv->initialized = false; |
| 82 | priv->on = false; |
Simon Glass | 5204e9b | 2017-05-18 20:09:48 -0600 | [diff] [blame] | 83 | priv->broken = dev_read_bool(dev, "broken"); |
Jean-Jacques Hiblot | 86322f5 | 2017-04-24 11:51:28 +0200 | [diff] [blame] | 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | static struct phy_ops sandbox_phy_ops = { |
| 89 | .power_on = sandbox_phy_power_on, |
| 90 | .power_off = sandbox_phy_power_off, |
| 91 | .init = sandbox_phy_init, |
| 92 | .exit = sandbox_phy_exit, |
| 93 | }; |
| 94 | |
| 95 | static const struct udevice_id sandbox_phy_ids[] = { |
| 96 | { .compatible = "sandbox,phy" }, |
| 97 | { } |
| 98 | }; |
| 99 | |
| 100 | U_BOOT_DRIVER(phy_sandbox) = { |
| 101 | .name = "phy_sandbox", |
| 102 | .id = UCLASS_PHY, |
| 103 | .of_match = sandbox_phy_ids, |
| 104 | .ops = &sandbox_phy_ops, |
| 105 | .probe = sandbox_phy_probe, |
| 106 | .priv_auto_alloc_size = sizeof(struct sandbox_phy_priv), |
| 107 | }; |