GNU Linux-libre 4.14.259-gnu1
[releases.git] / drivers / phy / ti / phy-da8xx-usb.c
1 /*
2  * phy-da8xx-usb - TI DaVinci DA8xx USB PHY driver
3  *
4  * Copyright (C) 2016 David Lechner <david@lechnology.com>
5  *
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.
9  *
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.
14  */
15
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>
25
26 #define PHY_INIT_BITS   (CFGCHIP2_SESENDEN | CFGCHIP2_VBDTCTEN)
27
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;
35 };
36
37 static int da8xx_usb11_phy_power_on(struct phy *phy)
38 {
39         struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
40         int ret;
41
42         ret = clk_prepare_enable(d_phy->usb11_clk);
43         if (ret)
44                 return ret;
45
46         regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_USB1SUSPENDM,
47                           CFGCHIP2_USB1SUSPENDM);
48
49         return 0;
50 }
51
52 static int da8xx_usb11_phy_power_off(struct phy *phy)
53 {
54         struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
55
56         regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_USB1SUSPENDM, 0);
57
58         clk_disable_unprepare(d_phy->usb11_clk);
59
60         return 0;
61 }
62
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,
67 };
68
69 static int da8xx_usb20_phy_power_on(struct phy *phy)
70 {
71         struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
72         int ret;
73
74         ret = clk_prepare_enable(d_phy->usb20_clk);
75         if (ret)
76                 return ret;
77
78         regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_OTGPWRDN, 0);
79
80         return 0;
81 }
82
83 static int da8xx_usb20_phy_power_off(struct phy *phy)
84 {
85         struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
86
87         regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_OTGPWRDN,
88                           CFGCHIP2_OTGPWRDN);
89
90         clk_disable_unprepare(d_phy->usb20_clk);
91
92         return 0;
93 }
94
95 static int da8xx_usb20_phy_set_mode(struct phy *phy, enum phy_mode mode)
96 {
97         struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
98         u32 val;
99
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;
112         }
113
114         regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_OTGMODE_MASK,
115                           val);
116
117         return 0;
118 }
119
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,
125 };
126
127 static struct phy *da8xx_usb_phy_of_xlate(struct device *dev,
128                                          struct of_phandle_args *args)
129 {
130         struct da8xx_usb_phy *d_phy = dev_get_drvdata(dev);
131
132         if (!d_phy)
133                 return ERR_PTR(-ENODEV);
134
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);
142         }
143 }
144
145 static int da8xx_usb_phy_probe(struct platform_device *pdev)
146 {
147         struct device           *dev = &pdev->dev;
148         struct device_node      *node = dev->of_node;
149         struct da8xx_usb_phy    *d_phy;
150
151         d_phy = devm_kzalloc(dev, sizeof(*d_phy), GFP_KERNEL);
152         if (!d_phy)
153                 return -ENOMEM;
154
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);
163         }
164
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);
169         }
170
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);
175         }
176
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);
181         }
182
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);
187         }
188
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);
192
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);
199                 }
200         } else {
201                 int ret;
202
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");
211         }
212
213         regmap_write_bits(d_phy->regmap, CFGCHIP(2),
214                           PHY_INIT_BITS, PHY_INIT_BITS);
215
216         return 0;
217 }
218
219 static int da8xx_usb_phy_remove(struct platform_device *pdev)
220 {
221         struct da8xx_usb_phy *d_phy = platform_get_drvdata(pdev);
222
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");
226         }
227
228         return 0;
229 }
230
231 static const struct of_device_id da8xx_usb_phy_ids[] = {
232         { .compatible = "ti,da830-usb-phy" },
233         { }
234 };
235 MODULE_DEVICE_TABLE(of, da8xx_usb_phy_ids);
236
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,
243         },
244 };
245
246 module_platform_driver(da8xx_usb_phy_driver);
247
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");