2 * phy-da8xx-usb - TI DaVinci DA8xx USB PHY driver
4 * Copyright (C) 2016 David Lechner <david@lechnology.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/clk.h>
19 #include <linux/mfd/da8xx-cfgchip.h>
20 #include <linux/mfd/syscon.h>
21 #include <linux/module.h>
22 #include <linux/phy/phy.h>
23 #include <linux/platform_device.h>
24 #include <linux/regmap.h>
26 #define PHY_INIT_BITS (CFGCHIP2_SESENDEN | CFGCHIP2_VBDTCTEN)
28 struct da8xx_usb_phy
{
29 struct phy_provider
*phy_provider
;
30 struct phy
*usb11_phy
;
31 struct phy
*usb20_phy
;
32 struct clk
*usb11_clk
;
33 struct clk
*usb20_clk
;
34 struct regmap
*regmap
;
37 static int da8xx_usb11_phy_power_on(struct phy
*phy
)
39 struct da8xx_usb_phy
*d_phy
= phy_get_drvdata(phy
);
42 ret
= clk_prepare_enable(d_phy
->usb11_clk
);
46 regmap_write_bits(d_phy
->regmap
, CFGCHIP(2), CFGCHIP2_USB1SUSPENDM
,
47 CFGCHIP2_USB1SUSPENDM
);
52 static int da8xx_usb11_phy_power_off(struct phy
*phy
)
54 struct da8xx_usb_phy
*d_phy
= phy_get_drvdata(phy
);
56 regmap_write_bits(d_phy
->regmap
, CFGCHIP(2), CFGCHIP2_USB1SUSPENDM
, 0);
58 clk_disable_unprepare(d_phy
->usb11_clk
);
63 static const struct phy_ops da8xx_usb11_phy_ops
= {
64 .power_on
= da8xx_usb11_phy_power_on
,
65 .power_off
= da8xx_usb11_phy_power_off
,
69 static int da8xx_usb20_phy_power_on(struct phy
*phy
)
71 struct da8xx_usb_phy
*d_phy
= phy_get_drvdata(phy
);
74 ret
= clk_prepare_enable(d_phy
->usb20_clk
);
78 regmap_write_bits(d_phy
->regmap
, CFGCHIP(2), CFGCHIP2_OTGPWRDN
, 0);
83 static int da8xx_usb20_phy_power_off(struct phy
*phy
)
85 struct da8xx_usb_phy
*d_phy
= phy_get_drvdata(phy
);
87 regmap_write_bits(d_phy
->regmap
, CFGCHIP(2), CFGCHIP2_OTGPWRDN
,
90 clk_disable_unprepare(d_phy
->usb20_clk
);
95 static int da8xx_usb20_phy_set_mode(struct phy
*phy
, enum phy_mode mode
)
97 struct da8xx_usb_phy
*d_phy
= phy_get_drvdata(phy
);
101 case PHY_MODE_USB_HOST
: /* Force VBUS valid, ID = 0 */
102 val
= CFGCHIP2_OTGMODE_FORCE_HOST
;
104 case PHY_MODE_USB_DEVICE
: /* Force VBUS valid, ID = 1 */
105 val
= CFGCHIP2_OTGMODE_FORCE_DEVICE
;
107 case PHY_MODE_USB_OTG
: /* Don't override the VBUS/ID comparators */
108 val
= CFGCHIP2_OTGMODE_NO_OVERRIDE
;
114 regmap_write_bits(d_phy
->regmap
, CFGCHIP(2), CFGCHIP2_OTGMODE_MASK
,
120 static const struct phy_ops da8xx_usb20_phy_ops
= {
121 .power_on
= da8xx_usb20_phy_power_on
,
122 .power_off
= da8xx_usb20_phy_power_off
,
123 .set_mode
= da8xx_usb20_phy_set_mode
,
124 .owner
= THIS_MODULE
,
127 static struct phy
*da8xx_usb_phy_of_xlate(struct device
*dev
,
128 struct of_phandle_args
*args
)
130 struct da8xx_usb_phy
*d_phy
= dev_get_drvdata(dev
);
133 return ERR_PTR(-ENODEV
);
135 switch (args
->args
[0]) {
137 return d_phy
->usb20_phy
;
139 return d_phy
->usb11_phy
;
141 return ERR_PTR(-EINVAL
);
145 static int da8xx_usb_phy_probe(struct platform_device
*pdev
)
147 struct device
*dev
= &pdev
->dev
;
148 struct device_node
*node
= dev
->of_node
;
149 struct da8xx_usb_phy
*d_phy
;
151 d_phy
= devm_kzalloc(dev
, sizeof(*d_phy
), GFP_KERNEL
);
156 d_phy
->regmap
= syscon_regmap_lookup_by_compatible(
159 d_phy
->regmap
= syscon_regmap_lookup_by_pdevname("syscon");
160 if (IS_ERR(d_phy
->regmap
)) {
161 dev_err(dev
, "Failed to get syscon\n");
162 return PTR_ERR(d_phy
->regmap
);
165 d_phy
->usb11_clk
= devm_clk_get(dev
, "usb11_phy");
166 if (IS_ERR(d_phy
->usb11_clk
)) {
167 dev_err(dev
, "Failed to get usb11_phy clock\n");
168 return PTR_ERR(d_phy
->usb11_clk
);
171 d_phy
->usb20_clk
= devm_clk_get(dev
, "usb20_phy");
172 if (IS_ERR(d_phy
->usb20_clk
)) {
173 dev_err(dev
, "Failed to get usb20_phy clock\n");
174 return PTR_ERR(d_phy
->usb20_clk
);
177 d_phy
->usb11_phy
= devm_phy_create(dev
, node
, &da8xx_usb11_phy_ops
);
178 if (IS_ERR(d_phy
->usb11_phy
)) {
179 dev_err(dev
, "Failed to create usb11 phy\n");
180 return PTR_ERR(d_phy
->usb11_phy
);
183 d_phy
->usb20_phy
= devm_phy_create(dev
, node
, &da8xx_usb20_phy_ops
);
184 if (IS_ERR(d_phy
->usb20_phy
)) {
185 dev_err(dev
, "Failed to create usb20 phy\n");
186 return PTR_ERR(d_phy
->usb20_phy
);
189 platform_set_drvdata(pdev
, d_phy
);
190 phy_set_drvdata(d_phy
->usb11_phy
, d_phy
);
191 phy_set_drvdata(d_phy
->usb20_phy
, d_phy
);
194 d_phy
->phy_provider
= devm_of_phy_provider_register(dev
,
195 da8xx_usb_phy_of_xlate
);
196 if (IS_ERR(d_phy
->phy_provider
)) {
197 dev_err(dev
, "Failed to create phy provider\n");
198 return PTR_ERR(d_phy
->phy_provider
);
203 ret
= phy_create_lookup(d_phy
->usb11_phy
, "usb-phy",
206 dev_warn(dev
, "Failed to create usb11 phy lookup\n");
207 ret
= phy_create_lookup(d_phy
->usb20_phy
, "usb-phy",
210 dev_warn(dev
, "Failed to create usb20 phy lookup\n");
213 regmap_write_bits(d_phy
->regmap
, CFGCHIP(2),
214 PHY_INIT_BITS
, PHY_INIT_BITS
);
219 static int da8xx_usb_phy_remove(struct platform_device
*pdev
)
221 struct da8xx_usb_phy
*d_phy
= platform_get_drvdata(pdev
);
223 if (!pdev
->dev
.of_node
) {
224 phy_remove_lookup(d_phy
->usb20_phy
, "usb-phy", "musb-da8xx");
225 phy_remove_lookup(d_phy
->usb11_phy
, "usb-phy", "ohci-da8xx");
231 static const struct of_device_id da8xx_usb_phy_ids
[] = {
232 { .compatible
= "ti,da830-usb-phy" },
235 MODULE_DEVICE_TABLE(of
, da8xx_usb_phy_ids
);
237 static struct platform_driver da8xx_usb_phy_driver
= {
238 .probe
= da8xx_usb_phy_probe
,
239 .remove
= da8xx_usb_phy_remove
,
241 .name
= "da8xx-usb-phy",
242 .of_match_table
= da8xx_usb_phy_ids
,
246 module_platform_driver(da8xx_usb_phy_driver
);
248 MODULE_ALIAS("platform:da8xx-usb-phy");
249 MODULE_AUTHOR("David Lechner <david@lechnology.com>");
250 MODULE_DESCRIPTION("TI DA8xx USB PHY driver");
251 MODULE_LICENSE("GPL v2");