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_data/phy-da8xx-usb.h>
24 #include <linux/platform_device.h>
25 #include <linux/regmap.h>
27 #define PHY_INIT_BITS (CFGCHIP2_SESENDEN | CFGCHIP2_VBDTCTEN)
29 struct da8xx_usb_phy
{
30 struct phy_provider
*phy_provider
;
31 struct phy
*usb11_phy
;
32 struct phy
*usb20_phy
;
33 struct clk
*usb11_clk
;
34 struct clk
*usb20_clk
;
35 struct regmap
*regmap
;
38 static int da8xx_usb11_phy_power_on(struct phy
*phy
)
40 struct da8xx_usb_phy
*d_phy
= phy_get_drvdata(phy
);
43 ret
= clk_prepare_enable(d_phy
->usb11_clk
);
47 regmap_write_bits(d_phy
->regmap
, CFGCHIP(2), CFGCHIP2_USB1SUSPENDM
,
48 CFGCHIP2_USB1SUSPENDM
);
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
);
64 static const struct phy_ops da8xx_usb11_phy_ops
= {
65 .power_on
= da8xx_usb11_phy_power_on
,
66 .power_off
= da8xx_usb11_phy_power_off
,
70 static int da8xx_usb20_phy_power_on(struct phy
*phy
)
72 struct da8xx_usb_phy
*d_phy
= phy_get_drvdata(phy
);
75 ret
= clk_prepare_enable(d_phy
->usb20_clk
);
79 regmap_write_bits(d_phy
->regmap
, CFGCHIP(2), CFGCHIP2_OTGPWRDN
, 0);
84 static int da8xx_usb20_phy_power_off(struct phy
*phy
)
86 struct da8xx_usb_phy
*d_phy
= phy_get_drvdata(phy
);
88 regmap_write_bits(d_phy
->regmap
, CFGCHIP(2), CFGCHIP2_OTGPWRDN
,
91 clk_disable_unprepare(d_phy
->usb20_clk
);
96 static int da8xx_usb20_phy_set_mode(struct phy
*phy
, enum phy_mode mode
)
98 struct da8xx_usb_phy
*d_phy
= phy_get_drvdata(phy
);
102 case PHY_MODE_USB_HOST
: /* Force VBUS valid, ID = 0 */
103 val
= CFGCHIP2_OTGMODE_FORCE_HOST
;
105 case PHY_MODE_USB_DEVICE
: /* Force VBUS valid, ID = 1 */
106 val
= CFGCHIP2_OTGMODE_FORCE_DEVICE
;
108 case PHY_MODE_USB_OTG
: /* Don't override the VBUS/ID comparators */
109 val
= CFGCHIP2_OTGMODE_NO_OVERRIDE
;
115 regmap_write_bits(d_phy
->regmap
, CFGCHIP(2), CFGCHIP2_OTGMODE_MASK
,
121 static const struct phy_ops da8xx_usb20_phy_ops
= {
122 .power_on
= da8xx_usb20_phy_power_on
,
123 .power_off
= da8xx_usb20_phy_power_off
,
124 .set_mode
= da8xx_usb20_phy_set_mode
,
125 .owner
= THIS_MODULE
,
128 static struct phy
*da8xx_usb_phy_of_xlate(struct device
*dev
,
129 struct of_phandle_args
*args
)
131 struct da8xx_usb_phy
*d_phy
= dev_get_drvdata(dev
);
134 return ERR_PTR(-ENODEV
);
136 switch (args
->args
[0]) {
138 return d_phy
->usb20_phy
;
140 return d_phy
->usb11_phy
;
142 return ERR_PTR(-EINVAL
);
146 static int da8xx_usb_phy_probe(struct platform_device
*pdev
)
148 struct device
*dev
= &pdev
->dev
;
149 struct da8xx_usb_phy_platform_data
*pdata
= dev
->platform_data
;
150 struct device_node
*node
= dev
->of_node
;
151 struct da8xx_usb_phy
*d_phy
;
153 d_phy
= devm_kzalloc(dev
, sizeof(*d_phy
), GFP_KERNEL
);
158 d_phy
->regmap
= pdata
->cfgchip
;
160 d_phy
->regmap
= syscon_regmap_lookup_by_compatible(
162 if (IS_ERR(d_phy
->regmap
)) {
163 dev_err(dev
, "Failed to get syscon\n");
164 return PTR_ERR(d_phy
->regmap
);
167 d_phy
->usb11_clk
= devm_clk_get(dev
, "usb1_clk48");
168 if (IS_ERR(d_phy
->usb11_clk
)) {
169 dev_err(dev
, "Failed to get usb1_clk48\n");
170 return PTR_ERR(d_phy
->usb11_clk
);
173 d_phy
->usb20_clk
= devm_clk_get(dev
, "usb0_clk48");
174 if (IS_ERR(d_phy
->usb20_clk
)) {
175 dev_err(dev
, "Failed to get usb0_clk48\n");
176 return PTR_ERR(d_phy
->usb20_clk
);
179 d_phy
->usb11_phy
= devm_phy_create(dev
, node
, &da8xx_usb11_phy_ops
);
180 if (IS_ERR(d_phy
->usb11_phy
)) {
181 dev_err(dev
, "Failed to create usb11 phy\n");
182 return PTR_ERR(d_phy
->usb11_phy
);
185 d_phy
->usb20_phy
= devm_phy_create(dev
, node
, &da8xx_usb20_phy_ops
);
186 if (IS_ERR(d_phy
->usb20_phy
)) {
187 dev_err(dev
, "Failed to create usb20 phy\n");
188 return PTR_ERR(d_phy
->usb20_phy
);
191 platform_set_drvdata(pdev
, d_phy
);
192 phy_set_drvdata(d_phy
->usb11_phy
, d_phy
);
193 phy_set_drvdata(d_phy
->usb20_phy
, d_phy
);
196 d_phy
->phy_provider
= devm_of_phy_provider_register(dev
,
197 da8xx_usb_phy_of_xlate
);
198 if (IS_ERR(d_phy
->phy_provider
)) {
199 dev_err(dev
, "Failed to create phy provider\n");
200 return PTR_ERR(d_phy
->phy_provider
);
205 ret
= phy_create_lookup(d_phy
->usb11_phy
, "usb-phy",
208 dev_warn(dev
, "Failed to create usb11 phy lookup\n");
209 ret
= phy_create_lookup(d_phy
->usb20_phy
, "usb-phy",
212 dev_warn(dev
, "Failed to create usb20 phy lookup\n");
215 regmap_write_bits(d_phy
->regmap
, CFGCHIP(2),
216 PHY_INIT_BITS
, PHY_INIT_BITS
);
221 static int da8xx_usb_phy_remove(struct platform_device
*pdev
)
223 struct da8xx_usb_phy
*d_phy
= platform_get_drvdata(pdev
);
225 if (!pdev
->dev
.of_node
) {
226 phy_remove_lookup(d_phy
->usb20_phy
, "usb-phy", "musb-da8xx");
227 phy_remove_lookup(d_phy
->usb11_phy
, "usb-phy", "ohci-da8xx");
233 static const struct of_device_id da8xx_usb_phy_ids
[] = {
234 { .compatible
= "ti,da830-usb-phy" },
237 MODULE_DEVICE_TABLE(of
, da8xx_usb_phy_ids
);
239 static struct platform_driver da8xx_usb_phy_driver
= {
240 .probe
= da8xx_usb_phy_probe
,
241 .remove
= da8xx_usb_phy_remove
,
243 .name
= "da8xx-usb-phy",
244 .of_match_table
= da8xx_usb_phy_ids
,
248 module_platform_driver(da8xx_usb_phy_driver
);
250 MODULE_ALIAS("platform:da8xx-usb-phy");
251 MODULE_AUTHOR("David Lechner <david@lechnology.com>");
252 MODULE_DESCRIPTION("TI DA8xx USB PHY driver");
253 MODULE_LICENSE("GPL v2");