Get rid of 'remove_new' relic from platform driver struct
[linux.git] / drivers / misc / hisi_hikey_usb.c
blobffe7b945a29857d42a73f6ea01c623f1f108f7fe
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/platform_device.h>
18 #include <linux/property.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/slab.h>
21 #include <linux/usb/role.h>
23 #define DEVICE_DRIVER_NAME "hisi_hikey_usb"
25 #define HUB_VBUS_POWER_ON 1
26 #define HUB_VBUS_POWER_OFF 0
27 #define USB_SWITCH_TO_HUB 1
28 #define USB_SWITCH_TO_TYPEC 0
29 #define TYPEC_VBUS_POWER_ON 1
30 #define TYPEC_VBUS_POWER_OFF 0
32 struct hisi_hikey_usb {
33 struct device *dev;
34 struct gpio_desc *otg_switch;
35 struct gpio_desc *typec_vbus;
36 struct gpio_desc *reset;
38 struct regulator *regulator;
40 struct usb_role_switch *hub_role_sw;
42 struct usb_role_switch *dev_role_sw;
43 enum usb_role role;
45 struct mutex lock;
46 struct work_struct work;
48 struct notifier_block nb;
51 static void hub_power_ctrl(struct hisi_hikey_usb *hisi_hikey_usb, int value)
53 int ret, status;
55 if (!hisi_hikey_usb->regulator)
56 return;
58 status = regulator_is_enabled(hisi_hikey_usb->regulator);
59 if (status == !!value)
60 return;
62 if (value)
63 ret = regulator_enable(hisi_hikey_usb->regulator);
64 else
65 ret = regulator_disable(hisi_hikey_usb->regulator);
67 if (ret)
68 dev_err(hisi_hikey_usb->dev,
69 "Can't switch regulator state to %s\n",
70 value ? "enabled" : "disabled");
73 static void usb_switch_ctrl(struct hisi_hikey_usb *hisi_hikey_usb,
74 int switch_to)
76 if (!hisi_hikey_usb->otg_switch)
77 return;
79 gpiod_set_value_cansleep(hisi_hikey_usb->otg_switch, switch_to);
82 static void usb_typec_power_ctrl(struct hisi_hikey_usb *hisi_hikey_usb,
83 int value)
85 if (!hisi_hikey_usb->typec_vbus)
86 return;
88 gpiod_set_value_cansleep(hisi_hikey_usb->typec_vbus, value);
91 static void relay_set_role_switch(struct work_struct *work)
93 struct hisi_hikey_usb *hisi_hikey_usb = container_of(work,
94 struct hisi_hikey_usb,
95 work);
96 struct usb_role_switch *sw;
97 enum usb_role role;
99 if (!hisi_hikey_usb || !hisi_hikey_usb->dev_role_sw)
100 return;
102 mutex_lock(&hisi_hikey_usb->lock);
103 switch (hisi_hikey_usb->role) {
104 case USB_ROLE_NONE:
105 usb_typec_power_ctrl(hisi_hikey_usb, TYPEC_VBUS_POWER_OFF);
106 usb_switch_ctrl(hisi_hikey_usb, USB_SWITCH_TO_HUB);
107 hub_power_ctrl(hisi_hikey_usb, HUB_VBUS_POWER_ON);
108 break;
109 case USB_ROLE_HOST:
110 hub_power_ctrl(hisi_hikey_usb, HUB_VBUS_POWER_OFF);
111 usb_switch_ctrl(hisi_hikey_usb, USB_SWITCH_TO_TYPEC);
112 usb_typec_power_ctrl(hisi_hikey_usb, TYPEC_VBUS_POWER_ON);
113 break;
114 case USB_ROLE_DEVICE:
115 hub_power_ctrl(hisi_hikey_usb, HUB_VBUS_POWER_OFF);
116 usb_typec_power_ctrl(hisi_hikey_usb, TYPEC_VBUS_POWER_OFF);
117 usb_switch_ctrl(hisi_hikey_usb, USB_SWITCH_TO_TYPEC);
118 break;
119 default:
120 break;
122 sw = hisi_hikey_usb->dev_role_sw;
123 role = hisi_hikey_usb->role;
124 mutex_unlock(&hisi_hikey_usb->lock);
126 usb_role_switch_set_role(sw, role);
129 static int hub_usb_role_switch_set(struct usb_role_switch *sw, enum usb_role role)
131 struct hisi_hikey_usb *hisi_hikey_usb = usb_role_switch_get_drvdata(sw);
133 if (!hisi_hikey_usb || !hisi_hikey_usb->dev_role_sw)
134 return -EINVAL;
136 mutex_lock(&hisi_hikey_usb->lock);
137 hisi_hikey_usb->role = role;
138 mutex_unlock(&hisi_hikey_usb->lock);
140 schedule_work(&hisi_hikey_usb->work);
142 return 0;
145 static int hisi_hikey_usb_of_role_switch(struct platform_device *pdev,
146 struct hisi_hikey_usb *hisi_hikey_usb)
148 struct device *dev = &pdev->dev;
149 struct usb_role_switch_desc hub_role_switch = {NULL};
151 if (!device_property_read_bool(dev, "usb-role-switch"))
152 return 0;
154 hisi_hikey_usb->otg_switch = devm_gpiod_get(dev, "otg-switch",
155 GPIOD_OUT_HIGH);
156 if (IS_ERR(hisi_hikey_usb->otg_switch)) {
157 dev_err(dev, "get otg-switch failed with error %ld\n",
158 PTR_ERR(hisi_hikey_usb->otg_switch));
159 return PTR_ERR(hisi_hikey_usb->otg_switch);
162 hisi_hikey_usb->typec_vbus = devm_gpiod_get(dev, "typec-vbus",
163 GPIOD_OUT_LOW);
164 if (IS_ERR(hisi_hikey_usb->typec_vbus)) {
165 dev_err(dev, "get typec-vbus failed with error %ld\n",
166 PTR_ERR(hisi_hikey_usb->typec_vbus));
167 return PTR_ERR(hisi_hikey_usb->typec_vbus);
170 hisi_hikey_usb->reset = devm_gpiod_get_optional(dev,
171 "hub-reset-en",
172 GPIOD_OUT_HIGH);
173 if (IS_ERR(hisi_hikey_usb->reset)) {
174 dev_err(dev, "get hub-reset-en failed with error %ld\n",
175 PTR_ERR(hisi_hikey_usb->reset));
176 return PTR_ERR(hisi_hikey_usb->reset);
179 hisi_hikey_usb->dev_role_sw = usb_role_switch_get(dev);
180 if (!hisi_hikey_usb->dev_role_sw)
181 return -EPROBE_DEFER;
182 if (IS_ERR(hisi_hikey_usb->dev_role_sw)) {
183 dev_err(dev, "get device role switch failed with error %ld\n",
184 PTR_ERR(hisi_hikey_usb->dev_role_sw));
185 return PTR_ERR(hisi_hikey_usb->dev_role_sw);
188 INIT_WORK(&hisi_hikey_usb->work, relay_set_role_switch);
190 hub_role_switch.fwnode = dev_fwnode(dev);
191 hub_role_switch.set = hub_usb_role_switch_set;
192 hub_role_switch.driver_data = hisi_hikey_usb;
194 hisi_hikey_usb->hub_role_sw = usb_role_switch_register(dev,
195 &hub_role_switch);
197 if (IS_ERR(hisi_hikey_usb->hub_role_sw)) {
198 dev_err(dev,
199 "failed to register hub role with error %ld\n",
200 PTR_ERR(hisi_hikey_usb->hub_role_sw));
201 usb_role_switch_put(hisi_hikey_usb->dev_role_sw);
202 return PTR_ERR(hisi_hikey_usb->hub_role_sw);
205 return 0;
208 static int hisi_hikey_usb_probe(struct platform_device *pdev)
210 struct device *dev = &pdev->dev;
211 struct hisi_hikey_usb *hisi_hikey_usb;
212 int ret;
214 hisi_hikey_usb = devm_kzalloc(dev, sizeof(*hisi_hikey_usb), GFP_KERNEL);
215 if (!hisi_hikey_usb)
216 return -ENOMEM;
218 hisi_hikey_usb->dev = &pdev->dev;
219 mutex_init(&hisi_hikey_usb->lock);
221 hisi_hikey_usb->regulator = devm_regulator_get(dev, "hub-vdd");
222 if (IS_ERR(hisi_hikey_usb->regulator)) {
223 if (PTR_ERR(hisi_hikey_usb->regulator) == -EPROBE_DEFER) {
224 dev_info(dev, "waiting for hub-vdd-supply\n");
225 return PTR_ERR(hisi_hikey_usb->regulator);
227 dev_err(dev, "get hub-vdd-supply failed with error %ld\n",
228 PTR_ERR(hisi_hikey_usb->regulator));
229 return PTR_ERR(hisi_hikey_usb->regulator);
232 ret = hisi_hikey_usb_of_role_switch(pdev, hisi_hikey_usb);
233 if (ret)
234 return ret;
236 platform_set_drvdata(pdev, hisi_hikey_usb);
238 return 0;
241 static void hisi_hikey_usb_remove(struct platform_device *pdev)
243 struct hisi_hikey_usb *hisi_hikey_usb = platform_get_drvdata(pdev);
245 if (hisi_hikey_usb->hub_role_sw) {
246 usb_role_switch_unregister(hisi_hikey_usb->hub_role_sw);
248 if (hisi_hikey_usb->dev_role_sw)
249 usb_role_switch_put(hisi_hikey_usb->dev_role_sw);
250 } else {
251 hub_power_ctrl(hisi_hikey_usb, HUB_VBUS_POWER_OFF);
255 static const struct of_device_id id_table_hisi_hikey_usb[] = {
256 { .compatible = "hisilicon,usbhub" },
259 MODULE_DEVICE_TABLE(of, id_table_hisi_hikey_usb);
261 static struct platform_driver hisi_hikey_usb_driver = {
262 .probe = hisi_hikey_usb_probe,
263 .remove = hisi_hikey_usb_remove,
264 .driver = {
265 .name = DEVICE_DRIVER_NAME,
266 .of_match_table = id_table_hisi_hikey_usb,
270 module_platform_driver(hisi_hikey_usb_driver);
272 MODULE_AUTHOR("Yu Chen <chenyu56@huawei.com>");
273 MODULE_DESCRIPTION("Driver Support for USB functionality of Hikey");
274 MODULE_LICENSE("GPL v2");