Merge tag 'usb-for-v4.12' of git://git.kernel.org/pub/scm/linux/kernel/git/balbi...
[linux/fpc-iii.git] / drivers / phy / phy-da8xx-usb.c
blob1b82bff6330fd2f10b51373253943af64da46fb0
1 /*
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>
17 #include <linux/io.h>
18 #include <linux/of.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);
40 int ret;
42 ret = clk_prepare_enable(d_phy->usb11_clk);
43 if (ret)
44 return ret;
46 regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_USB1SUSPENDM,
47 CFGCHIP2_USB1SUSPENDM);
49 return 0;
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);
60 return 0;
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,
66 .owner = THIS_MODULE,
69 static int da8xx_usb20_phy_power_on(struct phy *phy)
71 struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
72 int ret;
74 ret = clk_prepare_enable(d_phy->usb20_clk);
75 if (ret)
76 return ret;
78 regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_OTGPWRDN, 0);
80 return 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,
88 CFGCHIP2_OTGPWRDN);
90 clk_disable_unprepare(d_phy->usb20_clk);
92 return 0;
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);
98 u32 val;
100 switch (mode) {
101 case PHY_MODE_USB_HOST: /* Force VBUS valid, ID = 0 */
102 val = CFGCHIP2_OTGMODE_FORCE_HOST;
103 break;
104 case PHY_MODE_USB_DEVICE: /* Force VBUS valid, ID = 1 */
105 val = CFGCHIP2_OTGMODE_FORCE_DEVICE;
106 break;
107 case PHY_MODE_USB_OTG: /* Don't override the VBUS/ID comparators */
108 val = CFGCHIP2_OTGMODE_NO_OVERRIDE;
109 break;
110 default:
111 return -EINVAL;
114 regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_OTGMODE_MASK,
115 val);
117 return 0;
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);
132 if (!d_phy)
133 return ERR_PTR(-ENODEV);
135 switch (args->args[0]) {
136 case 0:
137 return d_phy->usb20_phy;
138 case 1:
139 return d_phy->usb11_phy;
140 default:
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);
152 if (!d_phy)
153 return -ENOMEM;
155 if (node)
156 d_phy->regmap = syscon_regmap_lookup_by_compatible(
157 "ti,da830-cfgchip");
158 else
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);
193 if (node) {
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);
200 } else {
201 int ret;
203 ret = phy_create_lookup(d_phy->usb11_phy, "usb-phy",
204 "ohci-da8xx");
205 if (ret)
206 dev_warn(dev, "Failed to create usb11 phy lookup\n");
207 ret = phy_create_lookup(d_phy->usb20_phy, "usb-phy",
208 "musb-da8xx");
209 if (ret)
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);
216 return 0;
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");
228 return 0;
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,
240 .driver = {
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");