2 * IMG Pistachio USB PHY driver
4 * Copyright (C) 2015 Google, Inc.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
11 #include <linux/clk.h>
12 #include <linux/delay.h>
14 #include <linux/kernel.h>
15 #include <linux/mfd/syscon.h>
16 #include <linux/module.h>
18 #include <linux/phy/phy.h>
19 #include <linux/platform_device.h>
20 #include <linux/regmap.h>
22 #include <dt-bindings/phy/phy-pistachio-usb.h>
24 #define USB_PHY_CONTROL1 0x04
25 #define USB_PHY_CONTROL1_FSEL_SHIFT 2
26 #define USB_PHY_CONTROL1_FSEL_MASK 0x7
28 #define USB_PHY_STRAP_CONTROL 0x10
29 #define USB_PHY_STRAP_CONTROL_REFCLK_SHIFT 4
30 #define USB_PHY_STRAP_CONTROL_REFCLK_MASK 0x3
32 #define USB_PHY_STATUS 0x14
33 #define USB_PHY_STATUS_RX_PHY_CLK BIT(9)
34 #define USB_PHY_STATUS_RX_UTMI_CLK BIT(8)
35 #define USB_PHY_STATUS_VBUS_FAULT BIT(7)
37 struct pistachio_usb_phy
{
39 struct regmap
*cr_top
;
44 static const unsigned long fsel_rate_map
[] = {
55 static int pistachio_usb_phy_power_on(struct phy
*phy
)
57 struct pistachio_usb_phy
*p_phy
= phy_get_drvdata(phy
);
58 unsigned long timeout
, rate
;
62 ret
= clk_prepare_enable(p_phy
->phy_clk
);
64 dev_err(p_phy
->dev
, "Failed to enable PHY clock: %d\n", ret
);
68 regmap_update_bits(p_phy
->cr_top
, USB_PHY_STRAP_CONTROL
,
69 USB_PHY_STRAP_CONTROL_REFCLK_MASK
<<
70 USB_PHY_STRAP_CONTROL_REFCLK_SHIFT
,
71 p_phy
->refclk
<< USB_PHY_STRAP_CONTROL_REFCLK_SHIFT
);
73 rate
= clk_get_rate(p_phy
->phy_clk
);
74 if (p_phy
->refclk
== REFCLK_XO_CRYSTAL
&& rate
!= 12000000) {
75 dev_err(p_phy
->dev
, "Unsupported rate for XO crystal: %ld\n",
81 for (i
= 0; i
< ARRAY_SIZE(fsel_rate_map
); i
++) {
82 if (rate
== fsel_rate_map
[i
])
85 if (i
== ARRAY_SIZE(fsel_rate_map
)) {
86 dev_err(p_phy
->dev
, "Unsupported clock rate: %lu\n", rate
);
91 regmap_update_bits(p_phy
->cr_top
, USB_PHY_CONTROL1
,
92 USB_PHY_CONTROL1_FSEL_MASK
<<
93 USB_PHY_CONTROL1_FSEL_SHIFT
,
94 i
<< USB_PHY_CONTROL1_FSEL_SHIFT
);
96 timeout
= jiffies
+ msecs_to_jiffies(200);
97 while (time_before(jiffies
, timeout
)) {
100 regmap_read(p_phy
->cr_top
, USB_PHY_STATUS
, &val
);
101 if (val
& USB_PHY_STATUS_VBUS_FAULT
) {
102 dev_err(p_phy
->dev
, "VBUS fault detected\n");
106 if ((val
& USB_PHY_STATUS_RX_PHY_CLK
) &&
107 (val
& USB_PHY_STATUS_RX_UTMI_CLK
))
109 usleep_range(1000, 1500);
112 dev_err(p_phy
->dev
, "Timed out waiting for PHY to power on\n");
116 clk_disable_unprepare(p_phy
->phy_clk
);
120 static int pistachio_usb_phy_power_off(struct phy
*phy
)
122 struct pistachio_usb_phy
*p_phy
= phy_get_drvdata(phy
);
124 clk_disable_unprepare(p_phy
->phy_clk
);
129 static const struct phy_ops pistachio_usb_phy_ops
= {
130 .power_on
= pistachio_usb_phy_power_on
,
131 .power_off
= pistachio_usb_phy_power_off
,
132 .owner
= THIS_MODULE
,
135 static int pistachio_usb_phy_probe(struct platform_device
*pdev
)
137 struct pistachio_usb_phy
*p_phy
;
138 struct phy_provider
*provider
;
142 p_phy
= devm_kzalloc(&pdev
->dev
, sizeof(*p_phy
), GFP_KERNEL
);
145 p_phy
->dev
= &pdev
->dev
;
146 platform_set_drvdata(pdev
, p_phy
);
148 p_phy
->cr_top
= syscon_regmap_lookup_by_phandle(p_phy
->dev
->of_node
,
150 if (IS_ERR(p_phy
->cr_top
)) {
151 dev_err(p_phy
->dev
, "Failed to get CR_TOP registers: %ld\n",
152 PTR_ERR(p_phy
->cr_top
));
153 return PTR_ERR(p_phy
->cr_top
);
156 p_phy
->phy_clk
= devm_clk_get(p_phy
->dev
, "usb_phy");
157 if (IS_ERR(p_phy
->phy_clk
)) {
158 dev_err(p_phy
->dev
, "Failed to get usb_phy clock: %ld\n",
159 PTR_ERR(p_phy
->phy_clk
));
160 return PTR_ERR(p_phy
->phy_clk
);
163 ret
= of_property_read_u32(p_phy
->dev
->of_node
, "img,refclk",
166 dev_err(p_phy
->dev
, "No reference clock selector specified\n");
170 phy
= devm_phy_create(p_phy
->dev
, NULL
, &pistachio_usb_phy_ops
);
172 dev_err(p_phy
->dev
, "Failed to create PHY: %ld\n",
176 phy_set_drvdata(phy
, p_phy
);
178 provider
= devm_of_phy_provider_register(p_phy
->dev
,
179 of_phy_simple_xlate
);
180 if (IS_ERR(provider
)) {
181 dev_err(p_phy
->dev
, "Failed to register PHY provider: %ld\n",
183 return PTR_ERR(provider
);
189 static const struct of_device_id pistachio_usb_phy_of_match
[] = {
190 { .compatible
= "img,pistachio-usb-phy", },
193 MODULE_DEVICE_TABLE(of
, pistachio_usb_phy_of_match
);
195 static struct platform_driver pistachio_usb_phy_driver
= {
196 .probe
= pistachio_usb_phy_probe
,
198 .name
= "pistachio-usb-phy",
199 .of_match_table
= pistachio_usb_phy_of_match
,
202 module_platform_driver(pistachio_usb_phy_driver
);
204 MODULE_AUTHOR("Andrew Bresticker <abrestic@chromium.org>");
205 MODULE_DESCRIPTION("IMG Pistachio USB2.0 PHY driver");
206 MODULE_LICENSE("GPL v2");