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/regmap.h>
19 #define PHY_INIT_BITS (CFGCHIP2_SESENDEN | CFGCHIP2_VBDTCTEN)
21 struct da8xx_usb_phy
{
22 struct phy_provider
*phy_provider
;
23 struct phy
*usb11_phy
;
24 struct phy
*usb20_phy
;
25 struct clk
*usb11_clk
;
26 struct clk
*usb20_clk
;
27 struct regmap
*regmap
;
30 static int da8xx_usb11_phy_power_on(struct phy
*phy
)
32 struct da8xx_usb_phy
*d_phy
= phy_get_drvdata(phy
);
35 ret
= clk_prepare_enable(d_phy
->usb11_clk
);
39 regmap_write_bits(d_phy
->regmap
, CFGCHIP(2), CFGCHIP2_USB1SUSPENDM
,
40 CFGCHIP2_USB1SUSPENDM
);
45 static int da8xx_usb11_phy_power_off(struct phy
*phy
)
47 struct da8xx_usb_phy
*d_phy
= phy_get_drvdata(phy
);
49 regmap_write_bits(d_phy
->regmap
, CFGCHIP(2), CFGCHIP2_USB1SUSPENDM
, 0);
51 clk_disable_unprepare(d_phy
->usb11_clk
);
56 static const struct phy_ops da8xx_usb11_phy_ops
= {
57 .power_on
= da8xx_usb11_phy_power_on
,
58 .power_off
= da8xx_usb11_phy_power_off
,
62 static int da8xx_usb20_phy_power_on(struct phy
*phy
)
64 struct da8xx_usb_phy
*d_phy
= phy_get_drvdata(phy
);
67 ret
= clk_prepare_enable(d_phy
->usb20_clk
);
71 regmap_write_bits(d_phy
->regmap
, CFGCHIP(2), CFGCHIP2_OTGPWRDN
, 0);
76 static int da8xx_usb20_phy_power_off(struct phy
*phy
)
78 struct da8xx_usb_phy
*d_phy
= phy_get_drvdata(phy
);
80 regmap_write_bits(d_phy
->regmap
, CFGCHIP(2), CFGCHIP2_OTGPWRDN
,
83 clk_disable_unprepare(d_phy
->usb20_clk
);
88 static int da8xx_usb20_phy_set_mode(struct phy
*phy
,
89 enum phy_mode mode
, int submode
)
91 struct da8xx_usb_phy
*d_phy
= phy_get_drvdata(phy
);
95 case PHY_MODE_USB_HOST
: /* Force VBUS valid, ID = 0 */
96 val
= CFGCHIP2_OTGMODE_FORCE_HOST
;
98 case PHY_MODE_USB_DEVICE
: /* Force VBUS valid, ID = 1 */
99 val
= CFGCHIP2_OTGMODE_FORCE_DEVICE
;
101 case PHY_MODE_USB_OTG
: /* Don't override the VBUS/ID comparators */
102 val
= CFGCHIP2_OTGMODE_NO_OVERRIDE
;
108 regmap_write_bits(d_phy
->regmap
, CFGCHIP(2), CFGCHIP2_OTGMODE_MASK
,
114 static const struct phy_ops da8xx_usb20_phy_ops
= {
115 .power_on
= da8xx_usb20_phy_power_on
,
116 .power_off
= da8xx_usb20_phy_power_off
,
117 .set_mode
= da8xx_usb20_phy_set_mode
,
118 .owner
= THIS_MODULE
,
121 static struct phy
*da8xx_usb_phy_of_xlate(struct device
*dev
,
122 struct of_phandle_args
*args
)
124 struct da8xx_usb_phy
*d_phy
= dev_get_drvdata(dev
);
127 return ERR_PTR(-ENODEV
);
129 switch (args
->args
[0]) {
131 return d_phy
->usb20_phy
;
133 return d_phy
->usb11_phy
;
135 return ERR_PTR(-EINVAL
);
139 static int da8xx_usb_phy_probe(struct platform_device
*pdev
)
141 struct device
*dev
= &pdev
->dev
;
142 struct da8xx_usb_phy_platform_data
*pdata
= dev
->platform_data
;
143 struct device_node
*node
= dev
->of_node
;
144 struct da8xx_usb_phy
*d_phy
;
146 d_phy
= devm_kzalloc(dev
, sizeof(*d_phy
), GFP_KERNEL
);
151 d_phy
->regmap
= pdata
->cfgchip
;
153 d_phy
->regmap
= syscon_regmap_lookup_by_compatible(
155 if (IS_ERR(d_phy
->regmap
)) {
156 dev_err(dev
, "Failed to get syscon\n");
157 return PTR_ERR(d_phy
->regmap
);
160 d_phy
->usb11_clk
= devm_clk_get(dev
, "usb1_clk48");
161 if (IS_ERR(d_phy
->usb11_clk
)) {
162 dev_err(dev
, "Failed to get usb1_clk48\n");
163 return PTR_ERR(d_phy
->usb11_clk
);
166 d_phy
->usb20_clk
= devm_clk_get(dev
, "usb0_clk48");
167 if (IS_ERR(d_phy
->usb20_clk
)) {
168 dev_err(dev
, "Failed to get usb0_clk48\n");
169 return PTR_ERR(d_phy
->usb20_clk
);
172 d_phy
->usb11_phy
= devm_phy_create(dev
, node
, &da8xx_usb11_phy_ops
);
173 if (IS_ERR(d_phy
->usb11_phy
)) {
174 dev_err(dev
, "Failed to create usb11 phy\n");
175 return PTR_ERR(d_phy
->usb11_phy
);
178 d_phy
->usb20_phy
= devm_phy_create(dev
, node
, &da8xx_usb20_phy_ops
);
179 if (IS_ERR(d_phy
->usb20_phy
)) {
180 dev_err(dev
, "Failed to create usb20 phy\n");
181 return PTR_ERR(d_phy
->usb20_phy
);
184 platform_set_drvdata(pdev
, d_phy
);
185 phy_set_drvdata(d_phy
->usb11_phy
, d_phy
);
186 phy_set_drvdata(d_phy
->usb20_phy
, d_phy
);
189 d_phy
->phy_provider
= devm_of_phy_provider_register(dev
,
190 da8xx_usb_phy_of_xlate
);
191 if (IS_ERR(d_phy
->phy_provider
)) {
192 dev_err(dev
, "Failed to create phy provider\n");
193 return PTR_ERR(d_phy
->phy_provider
);
198 ret
= phy_create_lookup(d_phy
->usb11_phy
, "usb-phy",
201 dev_warn(dev
, "Failed to create usb11 phy lookup\n");
202 ret
= phy_create_lookup(d_phy
->usb20_phy
, "usb-phy",
205 dev_warn(dev
, "Failed to create usb20 phy lookup\n");
208 regmap_write_bits(d_phy
->regmap
, CFGCHIP(2),
209 PHY_INIT_BITS
, PHY_INIT_BITS
);
214 static int da8xx_usb_phy_remove(struct platform_device
*pdev
)
216 struct da8xx_usb_phy
*d_phy
= platform_get_drvdata(pdev
);
218 if (!pdev
->dev
.of_node
) {
219 phy_remove_lookup(d_phy
->usb20_phy
, "usb-phy", "musb-da8xx");
220 phy_remove_lookup(d_phy
->usb11_phy
, "usb-phy", "ohci-da8xx");
226 static const struct of_device_id da8xx_usb_phy_ids
[] = {
227 { .compatible
= "ti,da830-usb-phy" },
230 MODULE_DEVICE_TABLE(of
, da8xx_usb_phy_ids
);
232 static struct platform_driver da8xx_usb_phy_driver
= {
233 .probe
= da8xx_usb_phy_probe
,
234 .remove
= da8xx_usb_phy_remove
,
236 .name
= "da8xx-usb-phy",
237 .of_match_table
= da8xx_usb_phy_ids
,
241 module_platform_driver(da8xx_usb_phy_driver
);
243 MODULE_ALIAS("platform:da8xx-usb-phy");
244 MODULE_AUTHOR("David Lechner <david@lechnology.com>");
245 MODULE_DESCRIPTION("TI DA8xx USB PHY driver");
246 MODULE_LICENSE("GPL v2");