Linux 4.18.10
[linux/fpc-iii.git] / drivers / phy / ti / phy-da8xx-usb.c
blobbefb886ff1212abe6796f4b67d43816987f5f70b
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_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);
41 int ret;
43 ret = clk_prepare_enable(d_phy->usb11_clk);
44 if (ret)
45 return ret;
47 regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_USB1SUSPENDM,
48 CFGCHIP2_USB1SUSPENDM);
50 return 0;
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);
61 return 0;
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,
67 .owner = THIS_MODULE,
70 static int da8xx_usb20_phy_power_on(struct phy *phy)
72 struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
73 int ret;
75 ret = clk_prepare_enable(d_phy->usb20_clk);
76 if (ret)
77 return ret;
79 regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_OTGPWRDN, 0);
81 return 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,
89 CFGCHIP2_OTGPWRDN);
91 clk_disable_unprepare(d_phy->usb20_clk);
93 return 0;
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);
99 u32 val;
101 switch (mode) {
102 case PHY_MODE_USB_HOST: /* Force VBUS valid, ID = 0 */
103 val = CFGCHIP2_OTGMODE_FORCE_HOST;
104 break;
105 case PHY_MODE_USB_DEVICE: /* Force VBUS valid, ID = 1 */
106 val = CFGCHIP2_OTGMODE_FORCE_DEVICE;
107 break;
108 case PHY_MODE_USB_OTG: /* Don't override the VBUS/ID comparators */
109 val = CFGCHIP2_OTGMODE_NO_OVERRIDE;
110 break;
111 default:
112 return -EINVAL;
115 regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_OTGMODE_MASK,
116 val);
118 return 0;
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);
133 if (!d_phy)
134 return ERR_PTR(-ENODEV);
136 switch (args->args[0]) {
137 case 0:
138 return d_phy->usb20_phy;
139 case 1:
140 return d_phy->usb11_phy;
141 default:
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);
154 if (!d_phy)
155 return -ENOMEM;
157 if (pdata)
158 d_phy->regmap = pdata->cfgchip;
159 else
160 d_phy->regmap = syscon_regmap_lookup_by_compatible(
161 "ti,da830-cfgchip");
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);
195 if (node) {
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);
202 } else {
203 int ret;
205 ret = phy_create_lookup(d_phy->usb11_phy, "usb-phy",
206 "ohci-da8xx");
207 if (ret)
208 dev_warn(dev, "Failed to create usb11 phy lookup\n");
209 ret = phy_create_lookup(d_phy->usb20_phy, "usb-phy",
210 "musb-da8xx");
211 if (ret)
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);
218 return 0;
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");
230 return 0;
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,
242 .driver = {
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");