1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Atheros AR71XX/9XXX USB PHY driver
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2015-2018 Alban Bedel <albeu@free.fr>
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <linux/module.h>
9*4882a593Smuzhiyun #include <linux/platform_device.h>
10*4882a593Smuzhiyun #include <linux/phy/phy.h>
11*4882a593Smuzhiyun #include <linux/reset.h>
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun struct ath79_usb_phy {
14*4882a593Smuzhiyun struct reset_control *reset;
15*4882a593Smuzhiyun /* The suspend override logic is inverted, hence the no prefix
16*4882a593Smuzhiyun * to make the code a bit easier to understand.
17*4882a593Smuzhiyun */
18*4882a593Smuzhiyun struct reset_control *no_suspend_override;
19*4882a593Smuzhiyun };
20*4882a593Smuzhiyun
ath79_usb_phy_power_on(struct phy * phy)21*4882a593Smuzhiyun static int ath79_usb_phy_power_on(struct phy *phy)
22*4882a593Smuzhiyun {
23*4882a593Smuzhiyun struct ath79_usb_phy *priv = phy_get_drvdata(phy);
24*4882a593Smuzhiyun int err = 0;
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun if (priv->no_suspend_override) {
27*4882a593Smuzhiyun err = reset_control_assert(priv->no_suspend_override);
28*4882a593Smuzhiyun if (err)
29*4882a593Smuzhiyun return err;
30*4882a593Smuzhiyun }
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun err = reset_control_deassert(priv->reset);
33*4882a593Smuzhiyun if (err && priv->no_suspend_override)
34*4882a593Smuzhiyun reset_control_deassert(priv->no_suspend_override);
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun return err;
37*4882a593Smuzhiyun }
38*4882a593Smuzhiyun
ath79_usb_phy_power_off(struct phy * phy)39*4882a593Smuzhiyun static int ath79_usb_phy_power_off(struct phy *phy)
40*4882a593Smuzhiyun {
41*4882a593Smuzhiyun struct ath79_usb_phy *priv = phy_get_drvdata(phy);
42*4882a593Smuzhiyun int err = 0;
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun err = reset_control_assert(priv->reset);
45*4882a593Smuzhiyun if (err)
46*4882a593Smuzhiyun return err;
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun if (priv->no_suspend_override) {
49*4882a593Smuzhiyun err = reset_control_deassert(priv->no_suspend_override);
50*4882a593Smuzhiyun if (err)
51*4882a593Smuzhiyun reset_control_deassert(priv->reset);
52*4882a593Smuzhiyun }
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun return err;
55*4882a593Smuzhiyun }
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun static const struct phy_ops ath79_usb_phy_ops = {
58*4882a593Smuzhiyun .power_on = ath79_usb_phy_power_on,
59*4882a593Smuzhiyun .power_off = ath79_usb_phy_power_off,
60*4882a593Smuzhiyun .owner = THIS_MODULE,
61*4882a593Smuzhiyun };
62*4882a593Smuzhiyun
ath79_usb_phy_probe(struct platform_device * pdev)63*4882a593Smuzhiyun static int ath79_usb_phy_probe(struct platform_device *pdev)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun struct ath79_usb_phy *priv;
66*4882a593Smuzhiyun struct phy *phy;
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
69*4882a593Smuzhiyun if (!priv)
70*4882a593Smuzhiyun return -ENOMEM;
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun priv->reset = devm_reset_control_get(&pdev->dev, "phy");
73*4882a593Smuzhiyun if (IS_ERR(priv->reset))
74*4882a593Smuzhiyun return PTR_ERR(priv->reset);
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun priv->no_suspend_override = devm_reset_control_get_optional(
77*4882a593Smuzhiyun &pdev->dev, "usb-suspend-override");
78*4882a593Smuzhiyun if (IS_ERR(priv->no_suspend_override))
79*4882a593Smuzhiyun return PTR_ERR(priv->no_suspend_override);
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun phy = devm_phy_create(&pdev->dev, NULL, &ath79_usb_phy_ops);
82*4882a593Smuzhiyun if (IS_ERR(phy))
83*4882a593Smuzhiyun return PTR_ERR(phy);
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun phy_set_drvdata(phy, priv);
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun return PTR_ERR_OR_ZERO(devm_of_phy_provider_register(
88*4882a593Smuzhiyun &pdev->dev, of_phy_simple_xlate));
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun static const struct of_device_id ath79_usb_phy_of_match[] = {
92*4882a593Smuzhiyun { .compatible = "qca,ar7100-usb-phy" },
93*4882a593Smuzhiyun {}
94*4882a593Smuzhiyun };
95*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, ath79_usb_phy_of_match);
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun static struct platform_driver ath79_usb_phy_driver = {
98*4882a593Smuzhiyun .probe = ath79_usb_phy_probe,
99*4882a593Smuzhiyun .driver = {
100*4882a593Smuzhiyun .of_match_table = ath79_usb_phy_of_match,
101*4882a593Smuzhiyun .name = "ath79-usb-phy",
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun };
104*4882a593Smuzhiyun module_platform_driver(ath79_usb_phy_driver);
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun MODULE_DESCRIPTION("ATH79 USB PHY driver");
107*4882a593Smuzhiyun MODULE_AUTHOR("Alban Bedel <albeu@free.fr>");
108*4882a593Smuzhiyun MODULE_LICENSE("GPL");
109