Merge remote-tracking branch 'stable/linux-5.15.y' into rpi-5.15.y
[platform/kernel/linux-rpi.git] / drivers / misc / hisi_hikey_usb.c
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
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
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
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
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
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
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         return PTR_ERR_OR_ZERO(hisi_hikey_usb->reset);
172 }
173
174 static int hisi_hikey_usb_probe(struct platform_device *pdev)
175 {
176         struct device *dev = &pdev->dev;
177         struct hisi_hikey_usb *hisi_hikey_usb;
178         struct usb_role_switch_desc hub_role_switch = {NULL};
179         int ret;
180
181         hisi_hikey_usb = devm_kzalloc(dev, sizeof(*hisi_hikey_usb), GFP_KERNEL);
182         if (!hisi_hikey_usb)
183                 return -ENOMEM;
184
185         hisi_hikey_usb->dev = &pdev->dev;
186
187         hisi_hikey_usb->otg_switch = devm_gpiod_get(dev, "otg-switch",
188                                                     GPIOD_OUT_HIGH);
189         if (IS_ERR(hisi_hikey_usb->otg_switch))
190                 return PTR_ERR(hisi_hikey_usb->otg_switch);
191
192         hisi_hikey_usb->typec_vbus = devm_gpiod_get(dev, "typec-vbus",
193                                                     GPIOD_OUT_LOW);
194         if (IS_ERR(hisi_hikey_usb->typec_vbus))
195                 return PTR_ERR(hisi_hikey_usb->typec_vbus);
196
197         /* Parse Kirin 970-specific OF data */
198         if (of_device_is_compatible(pdev->dev.of_node,
199                                     "hisilicon,kirin970_hikey_usbhub")) {
200                 ret = hisi_hikey_usb_parse_kirin970(pdev, hisi_hikey_usb);
201                 if (ret)
202                         return ret;
203         } else {
204                 /* hub-vdd33-en is optional */
205                 hisi_hikey_usb->hub_vbus = devm_gpiod_get_optional(dev, "hub-vdd33-en",
206                                                                    GPIOD_OUT_HIGH);
207                 if (IS_ERR(hisi_hikey_usb->hub_vbus))
208                         return PTR_ERR(hisi_hikey_usb->hub_vbus);
209         }
210
211         hisi_hikey_usb->dev_role_sw = usb_role_switch_get(dev);
212         if (!hisi_hikey_usb->dev_role_sw)
213                 return -EPROBE_DEFER;
214         if (IS_ERR(hisi_hikey_usb->dev_role_sw))
215                 return PTR_ERR(hisi_hikey_usb->dev_role_sw);
216
217         INIT_WORK(&hisi_hikey_usb->work, relay_set_role_switch);
218         mutex_init(&hisi_hikey_usb->lock);
219
220         hub_role_switch.fwnode = dev_fwnode(dev);
221         hub_role_switch.set = hub_usb_role_switch_set;
222         hub_role_switch.driver_data = hisi_hikey_usb;
223
224         hisi_hikey_usb->hub_role_sw = usb_role_switch_register(dev,
225                                                                &hub_role_switch);
226
227         if (IS_ERR(hisi_hikey_usb->hub_role_sw)) {
228                 usb_role_switch_put(hisi_hikey_usb->dev_role_sw);
229                 return PTR_ERR(hisi_hikey_usb->hub_role_sw);
230         }
231
232         platform_set_drvdata(pdev, hisi_hikey_usb);
233
234         return 0;
235 }
236
237 static int  hisi_hikey_usb_remove(struct platform_device *pdev)
238 {
239         struct hisi_hikey_usb *hisi_hikey_usb = platform_get_drvdata(pdev);
240
241         if (hisi_hikey_usb->hub_role_sw)
242                 usb_role_switch_unregister(hisi_hikey_usb->hub_role_sw);
243
244         if (hisi_hikey_usb->dev_role_sw)
245                 usb_role_switch_put(hisi_hikey_usb->dev_role_sw);
246
247         return 0;
248 }
249
250 static const struct of_device_id id_table_hisi_hikey_usb[] = {
251         { .compatible = "hisilicon,gpio_hubv1" },
252         { .compatible = "hisilicon,kirin970_hikey_usbhub" },
253         {}
254 };
255 MODULE_DEVICE_TABLE(of, id_table_hisi_hikey_usb);
256
257 static struct platform_driver hisi_hikey_usb_driver = {
258         .probe = hisi_hikey_usb_probe,
259         .remove = hisi_hikey_usb_remove,
260         .driver = {
261                 .name = DEVICE_DRIVER_NAME,
262                 .of_match_table = id_table_hisi_hikey_usb,
263         },
264 };
265
266 module_platform_driver(hisi_hikey_usb_driver);
267
268 MODULE_AUTHOR("Yu Chen <chenyu56@huawei.com>");
269 MODULE_DESCRIPTION("Driver Support for USB functionality of Hikey");
270 MODULE_LICENSE("GPL v2");