Merge tag 'firewire-fixes-6.9-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git...
[sfrench/cifs-2.6.git] / drivers / usb / misc / onboard_usb_hub.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Driver for onboard USB hubs
4  *
5  * Copyright (c) 2022, Google LLC
6  */
7
8 #include <linux/clk.h>
9 #include <linux/device.h>
10 #include <linux/export.h>
11 #include <linux/err.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/list.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/of.h>
19 #include <linux/of_platform.h>
20 #include <linux/platform_device.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/slab.h>
23 #include <linux/suspend.h>
24 #include <linux/sysfs.h>
25 #include <linux/usb.h>
26 #include <linux/usb/hcd.h>
27 #include <linux/usb/onboard_hub.h>
28 #include <linux/workqueue.h>
29
30 #include "onboard_usb_hub.h"
31
32 /*
33  * Use generic names, as the actual names might differ between hubs. If a new
34  * hub requires more than the currently supported supplies, add a new one here.
35  */
36 static const char * const supply_names[] = {
37         "vdd",
38         "vdd2",
39 };
40
41 #define MAX_SUPPLIES ARRAY_SIZE(supply_names)
42
43 static void onboard_hub_attach_usb_driver(struct work_struct *work);
44
45 static struct usb_device_driver onboard_hub_usbdev_driver;
46 static DECLARE_WORK(attach_usb_driver_work, onboard_hub_attach_usb_driver);
47
48 /************************** Platform driver **************************/
49
50 struct usbdev_node {
51         struct usb_device *udev;
52         struct list_head list;
53 };
54
55 struct onboard_hub {
56         struct regulator_bulk_data supplies[MAX_SUPPLIES];
57         struct device *dev;
58         const struct onboard_hub_pdata *pdata;
59         struct gpio_desc *reset_gpio;
60         bool always_powered_in_suspend;
61         bool is_powered_on;
62         bool going_away;
63         struct list_head udev_list;
64         struct mutex lock;
65         struct clk *clk;
66 };
67
68 static int onboard_hub_power_on(struct onboard_hub *hub)
69 {
70         int err;
71
72         err = clk_prepare_enable(hub->clk);
73         if (err) {
74                 dev_err(hub->dev, "failed to enable clock: %pe\n", ERR_PTR(err));
75                 return err;
76         }
77
78         err = regulator_bulk_enable(hub->pdata->num_supplies, hub->supplies);
79         if (err) {
80                 dev_err(hub->dev, "failed to enable supplies: %pe\n", ERR_PTR(err));
81                 return err;
82         }
83
84         fsleep(hub->pdata->reset_us);
85         gpiod_set_value_cansleep(hub->reset_gpio, 0);
86
87         hub->is_powered_on = true;
88
89         return 0;
90 }
91
92 static int onboard_hub_power_off(struct onboard_hub *hub)
93 {
94         int err;
95
96         gpiod_set_value_cansleep(hub->reset_gpio, 1);
97
98         err = regulator_bulk_disable(hub->pdata->num_supplies, hub->supplies);
99         if (err) {
100                 dev_err(hub->dev, "failed to disable supplies: %pe\n", ERR_PTR(err));
101                 return err;
102         }
103
104         clk_disable_unprepare(hub->clk);
105
106         hub->is_powered_on = false;
107
108         return 0;
109 }
110
111 static int __maybe_unused onboard_hub_suspend(struct device *dev)
112 {
113         struct onboard_hub *hub = dev_get_drvdata(dev);
114         struct usbdev_node *node;
115         bool power_off = true;
116
117         if (hub->always_powered_in_suspend)
118                 return 0;
119
120         mutex_lock(&hub->lock);
121
122         list_for_each_entry(node, &hub->udev_list, list) {
123                 if (!device_may_wakeup(node->udev->bus->controller))
124                         continue;
125
126                 if (usb_wakeup_enabled_descendants(node->udev)) {
127                         power_off = false;
128                         break;
129                 }
130         }
131
132         mutex_unlock(&hub->lock);
133
134         if (!power_off)
135                 return 0;
136
137         return onboard_hub_power_off(hub);
138 }
139
140 static int __maybe_unused onboard_hub_resume(struct device *dev)
141 {
142         struct onboard_hub *hub = dev_get_drvdata(dev);
143
144         if (hub->is_powered_on)
145                 return 0;
146
147         return onboard_hub_power_on(hub);
148 }
149
150 static inline void get_udev_link_name(const struct usb_device *udev, char *buf, size_t size)
151 {
152         snprintf(buf, size, "usb_dev.%s", dev_name(&udev->dev));
153 }
154
155 static int onboard_hub_add_usbdev(struct onboard_hub *hub, struct usb_device *udev)
156 {
157         struct usbdev_node *node;
158         char link_name[64];
159         int err;
160
161         mutex_lock(&hub->lock);
162
163         if (hub->going_away) {
164                 err = -EINVAL;
165                 goto error;
166         }
167
168         node = kzalloc(sizeof(*node), GFP_KERNEL);
169         if (!node) {
170                 err = -ENOMEM;
171                 goto error;
172         }
173
174         node->udev = udev;
175
176         list_add(&node->list, &hub->udev_list);
177
178         mutex_unlock(&hub->lock);
179
180         get_udev_link_name(udev, link_name, sizeof(link_name));
181         WARN_ON(sysfs_create_link(&hub->dev->kobj, &udev->dev.kobj, link_name));
182
183         return 0;
184
185 error:
186         mutex_unlock(&hub->lock);
187
188         return err;
189 }
190
191 static void onboard_hub_remove_usbdev(struct onboard_hub *hub, const struct usb_device *udev)
192 {
193         struct usbdev_node *node;
194         char link_name[64];
195
196         get_udev_link_name(udev, link_name, sizeof(link_name));
197         sysfs_remove_link(&hub->dev->kobj, link_name);
198
199         mutex_lock(&hub->lock);
200
201         list_for_each_entry(node, &hub->udev_list, list) {
202                 if (node->udev == udev) {
203                         list_del(&node->list);
204                         kfree(node);
205                         break;
206                 }
207         }
208
209         mutex_unlock(&hub->lock);
210 }
211
212 static ssize_t always_powered_in_suspend_show(struct device *dev, struct device_attribute *attr,
213                            char *buf)
214 {
215         const struct onboard_hub *hub = dev_get_drvdata(dev);
216
217         return sysfs_emit(buf, "%d\n", hub->always_powered_in_suspend);
218 }
219
220 static ssize_t always_powered_in_suspend_store(struct device *dev, struct device_attribute *attr,
221                             const char *buf, size_t count)
222 {
223         struct onboard_hub *hub = dev_get_drvdata(dev);
224         bool val;
225         int ret;
226
227         ret = kstrtobool(buf, &val);
228         if (ret < 0)
229                 return ret;
230
231         hub->always_powered_in_suspend = val;
232
233         return count;
234 }
235 static DEVICE_ATTR_RW(always_powered_in_suspend);
236
237 static struct attribute *onboard_hub_attrs[] = {
238         &dev_attr_always_powered_in_suspend.attr,
239         NULL,
240 };
241 ATTRIBUTE_GROUPS(onboard_hub);
242
243 static void onboard_hub_attach_usb_driver(struct work_struct *work)
244 {
245         int err;
246
247         err = driver_attach(&onboard_hub_usbdev_driver.driver);
248         if (err)
249                 pr_err("Failed to attach USB driver: %pe\n", ERR_PTR(err));
250 }
251
252 static int onboard_hub_probe(struct platform_device *pdev)
253 {
254         struct device *dev = &pdev->dev;
255         struct onboard_hub *hub;
256         unsigned int i;
257         int err;
258
259         hub = devm_kzalloc(dev, sizeof(*hub), GFP_KERNEL);
260         if (!hub)
261                 return -ENOMEM;
262
263         hub->pdata = device_get_match_data(dev);
264         if (!hub->pdata)
265                 return -EINVAL;
266
267         if (hub->pdata->num_supplies > MAX_SUPPLIES)
268                 return dev_err_probe(dev, -EINVAL, "max %zu supplies supported!\n",
269                                      MAX_SUPPLIES);
270
271         for (i = 0; i < hub->pdata->num_supplies; i++)
272                 hub->supplies[i].supply = supply_names[i];
273
274         err = devm_regulator_bulk_get(dev, hub->pdata->num_supplies, hub->supplies);
275         if (err) {
276                 dev_err(dev, "Failed to get regulator supplies: %pe\n", ERR_PTR(err));
277                 return err;
278         }
279
280         hub->clk = devm_clk_get_optional(dev, NULL);
281         if (IS_ERR(hub->clk))
282                 return dev_err_probe(dev, PTR_ERR(hub->clk), "failed to get clock\n");
283
284         hub->reset_gpio = devm_gpiod_get_optional(dev, "reset",
285                                                   GPIOD_OUT_HIGH);
286         if (IS_ERR(hub->reset_gpio))
287                 return dev_err_probe(dev, PTR_ERR(hub->reset_gpio), "failed to get reset GPIO\n");
288
289         hub->dev = dev;
290         mutex_init(&hub->lock);
291         INIT_LIST_HEAD(&hub->udev_list);
292
293         dev_set_drvdata(dev, hub);
294
295         err = onboard_hub_power_on(hub);
296         if (err)
297                 return err;
298
299         /*
300          * The USB driver might have been detached from the USB devices by
301          * onboard_hub_remove() (e.g. through an 'unbind' by userspace),
302          * make sure to re-attach it if needed.
303          *
304          * This needs to be done deferred to avoid self-deadlocks on systems
305          * with nested onboard hubs.
306          */
307         schedule_work(&attach_usb_driver_work);
308
309         return 0;
310 }
311
312 static void onboard_hub_remove(struct platform_device *pdev)
313 {
314         struct onboard_hub *hub = dev_get_drvdata(&pdev->dev);
315         struct usbdev_node *node;
316         struct usb_device *udev;
317
318         hub->going_away = true;
319
320         mutex_lock(&hub->lock);
321
322         /* unbind the USB devices to avoid dangling references to this device */
323         while (!list_empty(&hub->udev_list)) {
324                 node = list_first_entry(&hub->udev_list, struct usbdev_node, list);
325                 udev = node->udev;
326
327                 /*
328                  * Unbinding the driver will call onboard_hub_remove_usbdev(),
329                  * which acquires hub->lock.  We must release the lock first.
330                  */
331                 get_device(&udev->dev);
332                 mutex_unlock(&hub->lock);
333                 device_release_driver(&udev->dev);
334                 put_device(&udev->dev);
335                 mutex_lock(&hub->lock);
336         }
337
338         mutex_unlock(&hub->lock);
339
340         onboard_hub_power_off(hub);
341 }
342
343 MODULE_DEVICE_TABLE(of, onboard_hub_match);
344
345 static const struct dev_pm_ops __maybe_unused onboard_hub_pm_ops = {
346         SET_LATE_SYSTEM_SLEEP_PM_OPS(onboard_hub_suspend, onboard_hub_resume)
347 };
348
349 static struct platform_driver onboard_hub_driver = {
350         .probe = onboard_hub_probe,
351         .remove_new = onboard_hub_remove,
352
353         .driver = {
354                 .name = "onboard-usb-hub",
355                 .of_match_table = onboard_hub_match,
356                 .pm = pm_ptr(&onboard_hub_pm_ops),
357                 .dev_groups = onboard_hub_groups,
358         },
359 };
360
361 /************************** USB driver **************************/
362
363 #define VENDOR_ID_CYPRESS       0x04b4
364 #define VENDOR_ID_GENESYS       0x05e3
365 #define VENDOR_ID_MICROCHIP     0x0424
366 #define VENDOR_ID_REALTEK       0x0bda
367 #define VENDOR_ID_TI            0x0451
368 #define VENDOR_ID_VIA           0x2109
369
370 /*
371  * Returns the onboard_hub platform device that is associated with the USB
372  * device passed as parameter.
373  */
374 static struct onboard_hub *_find_onboard_hub(struct device *dev)
375 {
376         struct platform_device *pdev;
377         struct device_node *np;
378         struct onboard_hub *hub;
379
380         pdev = of_find_device_by_node(dev->of_node);
381         if (!pdev) {
382                 np = of_parse_phandle(dev->of_node, "peer-hub", 0);
383                 if (!np) {
384                         dev_err(dev, "failed to find device node for peer hub\n");
385                         return ERR_PTR(-EINVAL);
386                 }
387
388                 pdev = of_find_device_by_node(np);
389                 of_node_put(np);
390
391                 if (!pdev)
392                         return ERR_PTR(-ENODEV);
393         }
394
395         hub = dev_get_drvdata(&pdev->dev);
396         put_device(&pdev->dev);
397
398         /*
399          * The presence of drvdata ('hub') indicates that the platform driver
400          * finished probing. This handles the case where (conceivably) we could
401          * be running at the exact same time as the platform driver's probe. If
402          * we detect the race we request probe deferral and we'll come back and
403          * try again.
404          */
405         if (!hub)
406                 return ERR_PTR(-EPROBE_DEFER);
407
408         return hub;
409 }
410
411 static int onboard_hub_usbdev_probe(struct usb_device *udev)
412 {
413         struct device *dev = &udev->dev;
414         struct onboard_hub *hub;
415         int err;
416
417         /* ignore supported hubs without device tree node */
418         if (!dev->of_node)
419                 return -ENODEV;
420
421         hub = _find_onboard_hub(dev);
422         if (IS_ERR(hub))
423                 return PTR_ERR(hub);
424
425         dev_set_drvdata(dev, hub);
426
427         err = onboard_hub_add_usbdev(hub, udev);
428         if (err)
429                 return err;
430
431         return 0;
432 }
433
434 static void onboard_hub_usbdev_disconnect(struct usb_device *udev)
435 {
436         struct onboard_hub *hub = dev_get_drvdata(&udev->dev);
437
438         onboard_hub_remove_usbdev(hub, udev);
439 }
440
441 static const struct usb_device_id onboard_hub_id_table[] = {
442         { USB_DEVICE(VENDOR_ID_CYPRESS, 0x6504) }, /* CYUSB33{0,1,2}x/CYUSB230x 3.0 */
443         { USB_DEVICE(VENDOR_ID_CYPRESS, 0x6506) }, /* CYUSB33{0,1,2}x/CYUSB230x 2.0 */
444         { USB_DEVICE(VENDOR_ID_CYPRESS, 0x6570) }, /* CY7C6563x 2.0 */
445         { USB_DEVICE(VENDOR_ID_GENESYS, 0x0608) }, /* Genesys Logic GL850G USB 2.0 */
446         { USB_DEVICE(VENDOR_ID_GENESYS, 0x0610) }, /* Genesys Logic GL852G USB 2.0 */
447         { USB_DEVICE(VENDOR_ID_GENESYS, 0x0620) }, /* Genesys Logic GL3523 USB 3.1 */
448         { USB_DEVICE(VENDOR_ID_MICROCHIP, 0x2412) }, /* USB2412 USB 2.0 */
449         { USB_DEVICE(VENDOR_ID_MICROCHIP, 0x2514) }, /* USB2514B USB 2.0 */
450         { USB_DEVICE(VENDOR_ID_MICROCHIP, 0x2517) }, /* USB2517 USB 2.0 */
451         { USB_DEVICE(VENDOR_ID_MICROCHIP, 0x2744) }, /* USB5744 USB 2.0 */
452         { USB_DEVICE(VENDOR_ID_MICROCHIP, 0x5744) }, /* USB5744 USB 3.0 */
453         { USB_DEVICE(VENDOR_ID_REALTEK, 0x0411) }, /* RTS5411 USB 3.1 */
454         { USB_DEVICE(VENDOR_ID_REALTEK, 0x5411) }, /* RTS5411 USB 2.1 */
455         { USB_DEVICE(VENDOR_ID_REALTEK, 0x0414) }, /* RTS5414 USB 3.2 */
456         { USB_DEVICE(VENDOR_ID_REALTEK, 0x5414) }, /* RTS5414 USB 2.1 */
457         { USB_DEVICE(VENDOR_ID_TI, 0x8025) }, /* TI USB8020B 3.0 */
458         { USB_DEVICE(VENDOR_ID_TI, 0x8027) }, /* TI USB8020B 2.0 */
459         { USB_DEVICE(VENDOR_ID_TI, 0x8140) }, /* TI USB8041 3.0 */
460         { USB_DEVICE(VENDOR_ID_TI, 0x8142) }, /* TI USB8041 2.0 */
461         { USB_DEVICE(VENDOR_ID_VIA, 0x0817) }, /* VIA VL817 3.1 */
462         { USB_DEVICE(VENDOR_ID_VIA, 0x2817) }, /* VIA VL817 2.0 */
463         {}
464 };
465 MODULE_DEVICE_TABLE(usb, onboard_hub_id_table);
466
467 static struct usb_device_driver onboard_hub_usbdev_driver = {
468         .name = "onboard-usb-hub",
469         .probe = onboard_hub_usbdev_probe,
470         .disconnect = onboard_hub_usbdev_disconnect,
471         .generic_subclass = 1,
472         .supports_autosuspend = 1,
473         .id_table = onboard_hub_id_table,
474 };
475
476 static int __init onboard_hub_init(void)
477 {
478         int ret;
479
480         ret = usb_register_device_driver(&onboard_hub_usbdev_driver, THIS_MODULE);
481         if (ret)
482                 return ret;
483
484         ret = platform_driver_register(&onboard_hub_driver);
485         if (ret)
486                 usb_deregister_device_driver(&onboard_hub_usbdev_driver);
487
488         return ret;
489 }
490 module_init(onboard_hub_init);
491
492 static void __exit onboard_hub_exit(void)
493 {
494         usb_deregister_device_driver(&onboard_hub_usbdev_driver);
495         platform_driver_unregister(&onboard_hub_driver);
496
497         cancel_work_sync(&attach_usb_driver_work);
498 }
499 module_exit(onboard_hub_exit);
500
501 MODULE_AUTHOR("Matthias Kaehlcke <mka@chromium.org>");
502 MODULE_DESCRIPTION("Driver for discrete onboard USB hubs");
503 MODULE_LICENSE("GPL v2");