1 // SPDX-License-Identifier: GPL-2.0-only
3 * phy-da8xx-usb - TI DaVinci DA8xx USB PHY driver
5 * Copyright (C) 2016 David Lechner <david@lechnology.com>
11 #include <linux/mfd/da8xx-cfgchip.h>
12 #include <linux/mfd/syscon.h>
13 #include <linux/module.h>
14 #include <linux/phy/phy.h>
15 #include <linux/platform_data/phy-da8xx-usb.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/regmap.h>
20 #define PHY_INIT_BITS (CFGCHIP2_SESENDEN | CFGCHIP2_VBDTCTEN)
22 struct da8xx_usb_phy
{
24 struct phy_provider
*phy_provider
;
25 struct phy
*usb11_phy
;
26 struct phy
*usb20_phy
;
27 struct clk
*usb11_clk
;
28 struct clk
*usb20_clk
;
29 struct regmap
*regmap
;
32 static int da8xx_usb11_phy_power_on(struct phy
*phy
)
34 struct da8xx_usb_phy
*d_phy
= phy_get_drvdata(phy
);
37 ret
= clk_prepare_enable(d_phy
->usb11_clk
);
41 regmap_write_bits(d_phy
->regmap
, CFGCHIP(2), CFGCHIP2_USB1SUSPENDM
,
42 CFGCHIP2_USB1SUSPENDM
);
45 * USB1.1 can used USB2.0 output clock as reference clock so this is here to prevent USB2.0
46 * from shutting PHY's power when USB1.1 might use it
48 pm_runtime_get_sync(d_phy
->dev
);
53 static int da8xx_usb11_phy_power_off(struct phy
*phy
)
55 struct da8xx_usb_phy
*d_phy
= phy_get_drvdata(phy
);
57 regmap_write_bits(d_phy
->regmap
, CFGCHIP(2), CFGCHIP2_USB1SUSPENDM
, 0);
59 clk_disable_unprepare(d_phy
->usb11_clk
);
60 pm_runtime_put_sync(d_phy
->dev
);
65 static const struct phy_ops da8xx_usb11_phy_ops
= {
66 .power_on
= da8xx_usb11_phy_power_on
,
67 .power_off
= da8xx_usb11_phy_power_off
,
71 static int da8xx_usb20_phy_power_on(struct phy
*phy
)
73 struct da8xx_usb_phy
*d_phy
= phy_get_drvdata(phy
);
76 ret
= clk_prepare_enable(d_phy
->usb20_clk
);
80 regmap_write_bits(d_phy
->regmap
, CFGCHIP(2), CFGCHIP2_OTGPWRDN
, 0);
85 static int da8xx_usb20_phy_power_off(struct phy
*phy
)
87 struct da8xx_usb_phy
*d_phy
= phy_get_drvdata(phy
);
89 regmap_write_bits(d_phy
->regmap
, CFGCHIP(2), CFGCHIP2_OTGPWRDN
,
92 clk_disable_unprepare(d_phy
->usb20_clk
);
97 static int da8xx_usb20_phy_set_mode(struct phy
*phy
,
98 enum phy_mode mode
, int submode
)
100 struct da8xx_usb_phy
*d_phy
= phy_get_drvdata(phy
);
104 case PHY_MODE_USB_HOST
: /* Force VBUS valid, ID = 0 */
105 val
= CFGCHIP2_OTGMODE_FORCE_HOST
;
107 case PHY_MODE_USB_DEVICE
: /* Force VBUS valid, ID = 1 */
108 val
= CFGCHIP2_OTGMODE_FORCE_DEVICE
;
110 case PHY_MODE_USB_OTG
: /* Don't override the VBUS/ID comparators */
111 val
= CFGCHIP2_OTGMODE_NO_OVERRIDE
;
117 regmap_write_bits(d_phy
->regmap
, CFGCHIP(2), CFGCHIP2_OTGMODE_MASK
,
123 static const struct phy_ops da8xx_usb20_phy_ops
= {
124 .power_on
= da8xx_usb20_phy_power_on
,
125 .power_off
= da8xx_usb20_phy_power_off
,
126 .set_mode
= da8xx_usb20_phy_set_mode
,
127 .owner
= THIS_MODULE
,
130 static int __maybe_unused
da8xx_runtime_suspend(struct device
*dev
)
132 struct da8xx_usb_phy
*d_phy
= dev_get_drvdata(dev
);
134 dev_dbg(dev
, "Suspending ...\n");
136 regmap_set_bits(d_phy
->regmap
, CFGCHIP(2), CFGCHIP2_PHYPWRDN
| CFGCHIP2_OTGPWRDN
);
141 static int __maybe_unused
da8xx_runtime_resume(struct device
*dev
)
143 u32 mask
= CFGCHIP2_RESET
| CFGCHIP2_PHYPWRDN
| CFGCHIP2_OTGPWRDN
| CFGCHIP2_PHY_PLLON
;
144 struct da8xx_usb_phy
*d_phy
= dev_get_drvdata(dev
);
147 regmap_update_bits(d_phy
->regmap
, CFGCHIP(2), mask
, CFGCHIP2_PHY_PLLON
);
149 dev_dbg(dev
, "Resuming ...\n");
151 return regmap_read_poll_timeout(d_phy
->regmap
, CFGCHIP(2), pll_status
,
152 pll_status
& CFGCHIP2_PHYCLKGD
, 1000, 500000);
155 static const struct dev_pm_ops da8xx_usb_phy_pm_ops
= {
156 SET_RUNTIME_PM_OPS(da8xx_runtime_suspend
, da8xx_runtime_resume
, NULL
)
159 static struct phy
*da8xx_usb_phy_of_xlate(struct device
*dev
,
160 const struct of_phandle_args
*args
)
162 struct da8xx_usb_phy
*d_phy
= dev_get_drvdata(dev
);
165 return ERR_PTR(-ENODEV
);
167 switch (args
->args
[0]) {
169 return d_phy
->usb20_phy
;
171 return d_phy
->usb11_phy
;
173 return ERR_PTR(-EINVAL
);
177 static int da8xx_usb_phy_probe(struct platform_device
*pdev
)
179 struct device
*dev
= &pdev
->dev
;
180 struct da8xx_usb_phy_platform_data
*pdata
= dev
->platform_data
;
181 struct device_node
*node
= dev
->of_node
;
182 struct da8xx_usb_phy
*d_phy
;
184 d_phy
= devm_kzalloc(dev
, sizeof(*d_phy
), GFP_KERNEL
);
191 d_phy
->regmap
= pdata
->cfgchip
;
193 d_phy
->regmap
= syscon_regmap_lookup_by_compatible(
195 if (IS_ERR(d_phy
->regmap
)) {
196 dev_err(dev
, "Failed to get syscon\n");
197 return PTR_ERR(d_phy
->regmap
);
200 d_phy
->usb11_clk
= devm_clk_get(dev
, "usb1_clk48");
201 if (IS_ERR(d_phy
->usb11_clk
)) {
202 dev_err(dev
, "Failed to get usb1_clk48\n");
203 return PTR_ERR(d_phy
->usb11_clk
);
206 d_phy
->usb20_clk
= devm_clk_get(dev
, "usb0_clk48");
207 if (IS_ERR(d_phy
->usb20_clk
)) {
208 dev_err(dev
, "Failed to get usb0_clk48\n");
209 return PTR_ERR(d_phy
->usb20_clk
);
212 d_phy
->usb11_phy
= devm_phy_create(dev
, node
, &da8xx_usb11_phy_ops
);
213 if (IS_ERR(d_phy
->usb11_phy
)) {
214 dev_err(dev
, "Failed to create usb11 phy\n");
215 return PTR_ERR(d_phy
->usb11_phy
);
218 d_phy
->usb20_phy
= devm_phy_create(dev
, node
, &da8xx_usb20_phy_ops
);
219 if (IS_ERR(d_phy
->usb20_phy
)) {
220 dev_err(dev
, "Failed to create usb20 phy\n");
221 return PTR_ERR(d_phy
->usb20_phy
);
224 platform_set_drvdata(pdev
, d_phy
);
225 phy_set_drvdata(d_phy
->usb11_phy
, d_phy
);
226 phy_set_drvdata(d_phy
->usb20_phy
, d_phy
);
229 d_phy
->phy_provider
= devm_of_phy_provider_register(dev
,
230 da8xx_usb_phy_of_xlate
);
231 if (IS_ERR(d_phy
->phy_provider
)) {
232 dev_err(dev
, "Failed to create phy provider\n");
233 return PTR_ERR(d_phy
->phy_provider
);
238 ret
= phy_create_lookup(d_phy
->usb11_phy
, "usb-phy",
241 dev_warn(dev
, "Failed to create usb11 phy lookup\n");
242 ret
= phy_create_lookup(d_phy
->usb20_phy
, "usb-phy",
245 dev_warn(dev
, "Failed to create usb20 phy lookup\n");
248 regmap_write_bits(d_phy
->regmap
, CFGCHIP(2),
249 PHY_INIT_BITS
, PHY_INIT_BITS
);
251 pm_runtime_set_active(dev
);
252 devm_pm_runtime_enable(dev
);
254 * Prevent runtime pm from being ON by default. Users can enable
255 * it using power/control in sysfs.
257 pm_runtime_forbid(dev
);
262 static void da8xx_usb_phy_remove(struct platform_device
*pdev
)
264 struct da8xx_usb_phy
*d_phy
= platform_get_drvdata(pdev
);
266 if (!pdev
->dev
.of_node
) {
267 phy_remove_lookup(d_phy
->usb20_phy
, "usb-phy", "musb-da8xx");
268 phy_remove_lookup(d_phy
->usb11_phy
, "usb-phy", "ohci-da8xx");
272 static const struct of_device_id da8xx_usb_phy_ids
[] = {
273 { .compatible
= "ti,da830-usb-phy" },
276 MODULE_DEVICE_TABLE(of
, da8xx_usb_phy_ids
);
278 static struct platform_driver da8xx_usb_phy_driver
= {
279 .probe
= da8xx_usb_phy_probe
,
280 .remove
= da8xx_usb_phy_remove
,
282 .name
= "da8xx-usb-phy",
283 .pm
= &da8xx_usb_phy_pm_ops
,
284 .of_match_table
= da8xx_usb_phy_ids
,
288 module_platform_driver(da8xx_usb_phy_driver
);
290 MODULE_ALIAS("platform:da8xx-usb-phy");
291 MODULE_AUTHOR("David Lechner <david@lechnology.com>");
292 MODULE_DESCRIPTION("TI DA8xx USB PHY driver");
293 MODULE_LICENSE("GPL v2");