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 struct da8xx_usb_phy
{
27 struct phy_provider
*phy_provider
;
28 struct phy
*usb11_phy
;
29 struct phy
*usb20_phy
;
30 struct clk
*usb11_clk
;
31 struct clk
*usb20_clk
;
32 struct regmap
*regmap
;
35 static int da8xx_usb11_phy_power_on(struct phy
*phy
)
37 struct da8xx_usb_phy
*d_phy
= phy_get_drvdata(phy
);
40 ret
= clk_prepare_enable(d_phy
->usb11_clk
);
44 regmap_write_bits(d_phy
->regmap
, CFGCHIP(2), CFGCHIP2_USB1SUSPENDM
,
45 CFGCHIP2_USB1SUSPENDM
);
50 static int da8xx_usb11_phy_power_off(struct phy
*phy
)
52 struct da8xx_usb_phy
*d_phy
= phy_get_drvdata(phy
);
54 regmap_write_bits(d_phy
->regmap
, CFGCHIP(2), CFGCHIP2_USB1SUSPENDM
, 0);
56 clk_disable_unprepare(d_phy
->usb11_clk
);
61 static const struct phy_ops da8xx_usb11_phy_ops
= {
62 .power_on
= da8xx_usb11_phy_power_on
,
63 .power_off
= da8xx_usb11_phy_power_off
,
67 static int da8xx_usb20_phy_power_on(struct phy
*phy
)
69 struct da8xx_usb_phy
*d_phy
= phy_get_drvdata(phy
);
72 ret
= clk_prepare_enable(d_phy
->usb20_clk
);
76 regmap_write_bits(d_phy
->regmap
, CFGCHIP(2), CFGCHIP2_OTGPWRDN
, 0);
81 static int da8xx_usb20_phy_power_off(struct phy
*phy
)
83 struct da8xx_usb_phy
*d_phy
= phy_get_drvdata(phy
);
85 regmap_write_bits(d_phy
->regmap
, CFGCHIP(2), CFGCHIP2_OTGPWRDN
,
88 clk_disable_unprepare(d_phy
->usb20_clk
);
93 static int da8xx_usb20_phy_set_mode(struct phy
*phy
, enum phy_mode mode
)
95 struct da8xx_usb_phy
*d_phy
= phy_get_drvdata(phy
);
99 case PHY_MODE_USB_HOST
: /* Force VBUS valid, ID = 0 */
100 val
= CFGCHIP2_OTGMODE_FORCE_HOST
;
102 case PHY_MODE_USB_DEVICE
: /* Force VBUS valid, ID = 1 */
103 val
= CFGCHIP2_OTGMODE_FORCE_DEVICE
;
105 case PHY_MODE_USB_OTG
: /* Don't override the VBUS/ID comparators */
106 val
= CFGCHIP2_OTGMODE_NO_OVERRIDE
;
112 regmap_write_bits(d_phy
->regmap
, CFGCHIP(2), CFGCHIP2_OTGMODE_MASK
,
118 static const struct phy_ops da8xx_usb20_phy_ops
= {
119 .power_on
= da8xx_usb20_phy_power_on
,
120 .power_off
= da8xx_usb20_phy_power_off
,
121 .set_mode
= da8xx_usb20_phy_set_mode
,
122 .owner
= THIS_MODULE
,
125 static struct phy
*da8xx_usb_phy_of_xlate(struct device
*dev
,
126 struct of_phandle_args
*args
)
128 struct da8xx_usb_phy
*d_phy
= dev_get_drvdata(dev
);
131 return ERR_PTR(-ENODEV
);
133 switch (args
->args
[0]) {
135 return d_phy
->usb20_phy
;
137 return d_phy
->usb11_phy
;
139 return ERR_PTR(-EINVAL
);
143 static int da8xx_usb_phy_probe(struct platform_device
*pdev
)
145 struct device
*dev
= &pdev
->dev
;
146 struct device_node
*node
= dev
->of_node
;
147 struct da8xx_usb_phy
*d_phy
;
149 d_phy
= devm_kzalloc(dev
, sizeof(*d_phy
), GFP_KERNEL
);
154 d_phy
->regmap
= syscon_regmap_lookup_by_compatible(
157 d_phy
->regmap
= syscon_regmap_lookup_by_pdevname("syscon.0");
158 if (IS_ERR(d_phy
->regmap
)) {
159 dev_err(dev
, "Failed to get syscon\n");
160 return PTR_ERR(d_phy
->regmap
);
163 d_phy
->usb11_clk
= devm_clk_get(dev
, "usb11_phy");
164 if (IS_ERR(d_phy
->usb11_clk
)) {
165 dev_err(dev
, "Failed to get usb11_phy clock\n");
166 return PTR_ERR(d_phy
->usb11_clk
);
169 d_phy
->usb20_clk
= devm_clk_get(dev
, "usb20_phy");
170 if (IS_ERR(d_phy
->usb20_clk
)) {
171 dev_err(dev
, "Failed to get usb20_phy clock\n");
172 return PTR_ERR(d_phy
->usb20_clk
);
175 d_phy
->usb11_phy
= devm_phy_create(dev
, node
, &da8xx_usb11_phy_ops
);
176 if (IS_ERR(d_phy
->usb11_phy
)) {
177 dev_err(dev
, "Failed to create usb11 phy\n");
178 return PTR_ERR(d_phy
->usb11_phy
);
181 d_phy
->usb20_phy
= devm_phy_create(dev
, node
, &da8xx_usb20_phy_ops
);
182 if (IS_ERR(d_phy
->usb20_phy
)) {
183 dev_err(dev
, "Failed to create usb20 phy\n");
184 return PTR_ERR(d_phy
->usb20_phy
);
187 platform_set_drvdata(pdev
, d_phy
);
188 phy_set_drvdata(d_phy
->usb11_phy
, d_phy
);
189 phy_set_drvdata(d_phy
->usb20_phy
, d_phy
);
192 d_phy
->phy_provider
= devm_of_phy_provider_register(dev
,
193 da8xx_usb_phy_of_xlate
);
194 if (IS_ERR(d_phy
->phy_provider
)) {
195 dev_err(dev
, "Failed to create phy provider\n");
196 return PTR_ERR(d_phy
->phy_provider
);
201 ret
= phy_create_lookup(d_phy
->usb11_phy
, "usb-phy", "ohci.0");
203 dev_warn(dev
, "Failed to create usb11 phy lookup\n");
204 ret
= phy_create_lookup(d_phy
->usb20_phy
, "usb-phy",
207 dev_warn(dev
, "Failed to create usb20 phy lookup\n");
213 static int da8xx_usb_phy_remove(struct platform_device
*pdev
)
215 struct da8xx_usb_phy
*d_phy
= platform_get_drvdata(pdev
);
217 if (!pdev
->dev
.of_node
) {
218 phy_remove_lookup(d_phy
->usb20_phy
, "usb-phy", "musb-da8xx");
219 phy_remove_lookup(d_phy
->usb11_phy
, "usb-phy", "ohci.0");
225 static const struct of_device_id da8xx_usb_phy_ids
[] = {
226 { .compatible
= "ti,da830-usb-phy" },
229 MODULE_DEVICE_TABLE(of
, da8xx_usb_phy_ids
);
231 static struct platform_driver da8xx_usb_phy_driver
= {
232 .probe
= da8xx_usb_phy_probe
,
233 .remove
= da8xx_usb_phy_remove
,
235 .name
= "da8xx-usb-phy",
236 .of_match_table
= da8xx_usb_phy_ids
,
240 module_platform_driver(da8xx_usb_phy_driver
);
242 MODULE_ALIAS("platform:da8xx-usb-phy");
243 MODULE_AUTHOR("David Lechner <david@lechnology.com>");
244 MODULE_DESCRIPTION("TI DA8xx USB PHY driver");
245 MODULE_LICENSE("GPL v2");