Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / misc / hisi_hikey_usb.c
blob989d7d129469cfa2d2613344fc9a1ce97824ff16
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Support for usb functionality of Hikey series boards
4 * based on Hisilicon Kirin Soc.
6 * Copyright (C) 2017-2018 Hilisicon Electronics Co., Ltd.
7 * http://www.huawei.com
9 * Authors: Yu Chen <chenyu56@huawei.com>
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>
24 #define DEVICE_DRIVER_NAME "hisi_hikey_usb"
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
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;
40 struct regulator *regulator;
42 struct usb_role_switch *hub_role_sw;
44 struct usb_role_switch *dev_role_sw;
45 enum usb_role role;
47 struct mutex lock;
48 struct work_struct work;
50 struct notifier_block nb;
53 static void hub_power_ctrl(struct hisi_hikey_usb *hisi_hikey_usb, int value)
55 int ret, status;
57 if (hisi_hikey_usb->hub_vbus)
58 gpiod_set_value_cansleep(hisi_hikey_usb->hub_vbus, value);
60 if (!hisi_hikey_usb->regulator)
61 return;
63 status = regulator_is_enabled(hisi_hikey_usb->regulator);
64 if (status == !!value)
65 return;
67 if (value)
68 ret = regulator_enable(hisi_hikey_usb->regulator);
69 else
70 ret = regulator_disable(hisi_hikey_usb->regulator);
72 if (ret)
73 dev_err(hisi_hikey_usb->dev,
74 "Can't switch regulator state to %s\n",
75 value ? "enabled" : "disabled");
78 static void usb_switch_ctrl(struct hisi_hikey_usb *hisi_hikey_usb,
79 int switch_to)
81 if (!hisi_hikey_usb->otg_switch)
82 return;
84 gpiod_set_value_cansleep(hisi_hikey_usb->otg_switch, switch_to);
87 static void usb_typec_power_ctrl(struct hisi_hikey_usb *hisi_hikey_usb,
88 int value)
90 if (!hisi_hikey_usb->typec_vbus)
91 return;
93 gpiod_set_value_cansleep(hisi_hikey_usb->typec_vbus, value);
96 static void relay_set_role_switch(struct work_struct *work)
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;
104 if (!hisi_hikey_usb || !hisi_hikey_usb->dev_role_sw)
105 return;
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;
127 sw = hisi_hikey_usb->dev_role_sw;
128 role = hisi_hikey_usb->role;
129 mutex_unlock(&hisi_hikey_usb->lock);
131 usb_role_switch_set_role(sw, role);
134 static int hub_usb_role_switch_set(struct usb_role_switch *sw, enum usb_role role)
136 struct hisi_hikey_usb *hisi_hikey_usb = usb_role_switch_get_drvdata(sw);
138 if (!hisi_hikey_usb || !hisi_hikey_usb->dev_role_sw)
139 return -EINVAL;
141 mutex_lock(&hisi_hikey_usb->lock);
142 hisi_hikey_usb->role = role;
143 mutex_unlock(&hisi_hikey_usb->lock);
145 schedule_work(&hisi_hikey_usb->work);
147 return 0;
150 static int hisi_hikey_usb_parse_kirin970(struct platform_device *pdev,
151 struct hisi_hikey_usb *hisi_hikey_usb)
153 struct regulator *regulator;
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);
162 dev_err(&pdev->dev,
163 "get hub-vdd-supply failed with error %ld\n",
164 PTR_ERR(regulator));
165 return PTR_ERR(regulator);
167 hisi_hikey_usb->regulator = regulator;
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);
174 static int hisi_hikey_usb_probe(struct platform_device *pdev)
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;
181 hisi_hikey_usb = devm_kzalloc(dev, sizeof(*hisi_hikey_usb), GFP_KERNEL);
182 if (!hisi_hikey_usb)
183 return -ENOMEM;
185 hisi_hikey_usb->dev = &pdev->dev;
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);
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);
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);
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);
217 INIT_WORK(&hisi_hikey_usb->work, relay_set_role_switch);
218 mutex_init(&hisi_hikey_usb->lock);
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;
224 hisi_hikey_usb->hub_role_sw = usb_role_switch_register(dev,
225 &hub_role_switch);
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);
232 platform_set_drvdata(pdev, hisi_hikey_usb);
234 return 0;
237 static int hisi_hikey_usb_remove(struct platform_device *pdev)
239 struct hisi_hikey_usb *hisi_hikey_usb = platform_get_drvdata(pdev);
241 if (hisi_hikey_usb->hub_role_sw)
242 usb_role_switch_unregister(hisi_hikey_usb->hub_role_sw);
244 if (hisi_hikey_usb->dev_role_sw)
245 usb_role_switch_put(hisi_hikey_usb->dev_role_sw);
247 return 0;
250 static const struct of_device_id id_table_hisi_hikey_usb[] = {
251 { .compatible = "hisilicon,gpio_hubv1" },
252 { .compatible = "hisilicon,kirin970_hikey_usbhub" },
255 MODULE_DEVICE_TABLE(of, id_table_hisi_hikey_usb);
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,
266 module_platform_driver(hisi_hikey_usb_driver);
268 MODULE_AUTHOR("Yu Chen <chenyu56@huawei.com>");
269 MODULE_DESCRIPTION("Driver Support for USB functionality of Hikey");
270 MODULE_LICENSE("GPL v2");