1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Support for usb functionality of Hikey series boards
4 * based on Hisilicon Kirin Soc.
5 *
6 * Copyright (C) 2017-2018 Hilisicon Electronics Co., Ltd.
7 * http://www.huawei.com
8 *
9 * Authors: Yu Chen <chenyu56@huawei.com>
10 */
11
12 #include <linux/gpio/consumer.h>
13 #include <linux/kernel.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/module.h>
16 #include <linux/notifier.h>
17 #include <linux/of_gpio.h>
18 #include <linux/platform_device.h>
19 #include <linux/property.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/slab.h>
22 #include <linux/usb/role.h>
23
24 #define DEVICE_DRIVER_NAME "hisi_hikey_usb"
25
26 #define HUB_VBUS_POWER_ON 1
27 #define HUB_VBUS_POWER_OFF 0
28 #define USB_SWITCH_TO_HUB 1
29 #define USB_SWITCH_TO_TYPEC 0
30 #define TYPEC_VBUS_POWER_ON 1
31 #define TYPEC_VBUS_POWER_OFF 0
32
33 struct hisi_hikey_usb {
34 struct device *dev;
35 struct gpio_desc *otg_switch;
36 struct gpio_desc *typec_vbus;
37 struct gpio_desc *hub_vbus;
38 struct gpio_desc *reset;
39
40 struct regulator *regulator;
41
42 struct usb_role_switch *hub_role_sw;
43
44 struct usb_role_switch *dev_role_sw;
45 enum usb_role role;
46
47 struct mutex lock;
48 struct work_struct work;
49
50 struct notifier_block nb;
51 };
52
hub_power_ctrl(struct hisi_hikey_usb * hisi_hikey_usb,int value)53 static void hub_power_ctrl(struct hisi_hikey_usb *hisi_hikey_usb, int value)
54 {
55 int ret, status;
56
57 if (hisi_hikey_usb->hub_vbus)
58 gpiod_set_value_cansleep(hisi_hikey_usb->hub_vbus, value);
59
60 if (!hisi_hikey_usb->regulator)
61 return;
62
63 status = regulator_is_enabled(hisi_hikey_usb->regulator);
64 if (status == !!value)
65 return;
66
67 if (value)
68 ret = regulator_enable(hisi_hikey_usb->regulator);
69 else
70 ret = regulator_disable(hisi_hikey_usb->regulator);
71
72 if (ret)
73 dev_err(hisi_hikey_usb->dev,
74 "Can't switch regulator state to %s\n",
75 value ? "enabled" : "disabled");
76 }
77
usb_switch_ctrl(struct hisi_hikey_usb * hisi_hikey_usb,int switch_to)78 static void usb_switch_ctrl(struct hisi_hikey_usb *hisi_hikey_usb,
79 int switch_to)
80 {
81 if (!hisi_hikey_usb->otg_switch)
82 return;
83
84 gpiod_set_value_cansleep(hisi_hikey_usb->otg_switch, switch_to);
85 }
86
usb_typec_power_ctrl(struct hisi_hikey_usb * hisi_hikey_usb,int value)87 static void usb_typec_power_ctrl(struct hisi_hikey_usb *hisi_hikey_usb,
88 int value)
89 {
90 if (!hisi_hikey_usb->typec_vbus)
91 return;
92
93 gpiod_set_value_cansleep(hisi_hikey_usb->typec_vbus, value);
94 }
95
relay_set_role_switch(struct work_struct * work)96 static void relay_set_role_switch(struct work_struct *work)
97 {
98 struct hisi_hikey_usb *hisi_hikey_usb = container_of(work,
99 struct hisi_hikey_usb,
100 work);
101 struct usb_role_switch *sw;
102 enum usb_role role;
103
104 if (!hisi_hikey_usb || !hisi_hikey_usb->dev_role_sw)
105 return;
106
107 mutex_lock(&hisi_hikey_usb->lock);
108 switch (hisi_hikey_usb->role) {
109 case USB_ROLE_NONE:
110 usb_typec_power_ctrl(hisi_hikey_usb, TYPEC_VBUS_POWER_OFF);
111 usb_switch_ctrl(hisi_hikey_usb, USB_SWITCH_TO_HUB);
112 hub_power_ctrl(hisi_hikey_usb, HUB_VBUS_POWER_ON);
113 break;
114 case USB_ROLE_HOST:
115 hub_power_ctrl(hisi_hikey_usb, HUB_VBUS_POWER_OFF);
116 usb_switch_ctrl(hisi_hikey_usb, USB_SWITCH_TO_TYPEC);
117 usb_typec_power_ctrl(hisi_hikey_usb, TYPEC_VBUS_POWER_ON);
118 break;
119 case USB_ROLE_DEVICE:
120 hub_power_ctrl(hisi_hikey_usb, HUB_VBUS_POWER_OFF);
121 usb_typec_power_ctrl(hisi_hikey_usb, TYPEC_VBUS_POWER_OFF);
122 usb_switch_ctrl(hisi_hikey_usb, USB_SWITCH_TO_TYPEC);
123 break;
124 default:
125 break;
126 }
127 sw = hisi_hikey_usb->dev_role_sw;
128 role = hisi_hikey_usb->role;
129 mutex_unlock(&hisi_hikey_usb->lock);
130
131 usb_role_switch_set_role(sw, role);
132 }
133
hub_usb_role_switch_set(struct usb_role_switch * sw,enum usb_role role)134 static int hub_usb_role_switch_set(struct usb_role_switch *sw, enum usb_role role)
135 {
136 struct hisi_hikey_usb *hisi_hikey_usb = usb_role_switch_get_drvdata(sw);
137
138 if (!hisi_hikey_usb || !hisi_hikey_usb->dev_role_sw)
139 return -EINVAL;
140
141 mutex_lock(&hisi_hikey_usb->lock);
142 hisi_hikey_usb->role = role;
143 mutex_unlock(&hisi_hikey_usb->lock);
144
145 schedule_work(&hisi_hikey_usb->work);
146
147 return 0;
148 }
149
hisi_hikey_usb_parse_kirin970(struct platform_device * pdev,struct hisi_hikey_usb * hisi_hikey_usb)150 static int hisi_hikey_usb_parse_kirin970(struct platform_device *pdev,
151 struct hisi_hikey_usb *hisi_hikey_usb)
152 {
153 struct regulator *regulator;
154
155 regulator = devm_regulator_get(&pdev->dev, "hub-vdd");
156 if (IS_ERR(regulator)) {
157 if (PTR_ERR(regulator) == -EPROBE_DEFER) {
158 dev_info(&pdev->dev,
159 "waiting for hub-vdd-supply to be probed\n");
160 return PTR_ERR(regulator);
161 }
162 dev_err(&pdev->dev,
163 "get hub-vdd-supply failed with error %ld\n",
164 PTR_ERR(regulator));
165 return PTR_ERR(regulator);
166 }
167 hisi_hikey_usb->regulator = regulator;
168
169 hisi_hikey_usb->reset = devm_gpiod_get(&pdev->dev, "hub_reset_en_gpio",
170 GPIOD_OUT_HIGH);
171 if (IS_ERR(hisi_hikey_usb->reset))
172 return PTR_ERR(hisi_hikey_usb->reset);
173
174 return 0;
175 }
176
hisi_hikey_usb_probe(struct platform_device * pdev)177 static int hisi_hikey_usb_probe(struct platform_device *pdev)
178 {
179 struct device *dev = &pdev->dev;
180 struct hisi_hikey_usb *hisi_hikey_usb;
181 struct usb_role_switch_desc hub_role_switch = {NULL};
182 int ret;
183
184 hisi_hikey_usb = devm_kzalloc(dev, sizeof(*hisi_hikey_usb), GFP_KERNEL);
185 if (!hisi_hikey_usb)
186 return -ENOMEM;
187
188 hisi_hikey_usb->dev = &pdev->dev;
189
190 hisi_hikey_usb->otg_switch = devm_gpiod_get(dev, "otg-switch",
191 GPIOD_OUT_HIGH);
192 if (IS_ERR(hisi_hikey_usb->otg_switch))
193 return PTR_ERR(hisi_hikey_usb->otg_switch);
194
195 hisi_hikey_usb->typec_vbus = devm_gpiod_get(dev, "typec-vbus",
196 GPIOD_OUT_LOW);
197 if (IS_ERR(hisi_hikey_usb->typec_vbus))
198 return PTR_ERR(hisi_hikey_usb->typec_vbus);
199
200 /* Parse Kirin 970-specific OF data */
201 if (of_device_is_compatible(pdev->dev.of_node,
202 "hisilicon,kirin970_hikey_usbhub")) {
203 ret = hisi_hikey_usb_parse_kirin970(pdev, hisi_hikey_usb);
204 if (ret)
205 return ret;
206 } else {
207 /* hub-vdd33-en is optional */
208 hisi_hikey_usb->hub_vbus = devm_gpiod_get_optional(dev, "hub-vdd33-en",
209 GPIOD_OUT_HIGH);
210 if (IS_ERR(hisi_hikey_usb->hub_vbus))
211 return PTR_ERR(hisi_hikey_usb->hub_vbus);
212 }
213
214 hisi_hikey_usb->dev_role_sw = usb_role_switch_get(dev);
215 if (!hisi_hikey_usb->dev_role_sw)
216 return -EPROBE_DEFER;
217 if (IS_ERR(hisi_hikey_usb->dev_role_sw))
218 return PTR_ERR(hisi_hikey_usb->dev_role_sw);
219
220 INIT_WORK(&hisi_hikey_usb->work, relay_set_role_switch);
221 mutex_init(&hisi_hikey_usb->lock);
222
223 hub_role_switch.fwnode = dev_fwnode(dev);
224 hub_role_switch.set = hub_usb_role_switch_set;
225 hub_role_switch.driver_data = hisi_hikey_usb;
226
227 hisi_hikey_usb->hub_role_sw = usb_role_switch_register(dev,
228 &hub_role_switch);
229
230 if (IS_ERR(hisi_hikey_usb->hub_role_sw)) {
231 usb_role_switch_put(hisi_hikey_usb->dev_role_sw);
232 return PTR_ERR(hisi_hikey_usb->hub_role_sw);
233 }
234
235 platform_set_drvdata(pdev, hisi_hikey_usb);
236
237 return 0;
238 }
239
hisi_hikey_usb_remove(struct platform_device * pdev)240 static int hisi_hikey_usb_remove(struct platform_device *pdev)
241 {
242 struct hisi_hikey_usb *hisi_hikey_usb = platform_get_drvdata(pdev);
243
244 if (hisi_hikey_usb->hub_role_sw)
245 usb_role_switch_unregister(hisi_hikey_usb->hub_role_sw);
246
247 if (hisi_hikey_usb->dev_role_sw)
248 usb_role_switch_put(hisi_hikey_usb->dev_role_sw);
249
250 return 0;
251 }
252
253 static const struct of_device_id id_table_hisi_hikey_usb[] = {
254 { .compatible = "hisilicon,gpio_hubv1" },
255 { .compatible = "hisilicon,kirin970_hikey_usbhub" },
256 {}
257 };
258 MODULE_DEVICE_TABLE(of, id_table_hisi_hikey_usb);
259
260 static struct platform_driver hisi_hikey_usb_driver = {
261 .probe = hisi_hikey_usb_probe,
262 .remove = hisi_hikey_usb_remove,
263 .driver = {
264 .name = DEVICE_DRIVER_NAME,
265 .of_match_table = id_table_hisi_hikey_usb,
266 },
267 };
268
269 module_platform_driver(hisi_hikey_usb_driver);
270
271 MODULE_AUTHOR("Yu Chen <chenyu56@huawei.com>");
272 MODULE_DESCRIPTION("Driver Support for USB functionality of Hikey");
273 MODULE_LICENSE("GPL v2");
274