hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/drivers/usb/chipidea/core.c
....@@ -3,42 +3,16 @@
33 * core.c - ChipIdea USB IP core family device controller
44 *
55 * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved.
6
+ * Copyright (C) 2020 NXP
67 *
78 * Author: David Lopo
8
- */
9
-
10
-/*
11
- * Description: ChipIdea USB IP core family device controller
9
+ * Peter Chen <peter.chen@nxp.com>
1210 *
13
- * This driver is composed of several blocks:
14
- * - HW: hardware interface
15
- * - DBG: debug facilities (optional)
16
- * - UTIL: utilities
17
- * - ISR: interrupts handling
18
- * - ENDPT: endpoint operations (Gadget API)
19
- * - GADGET: gadget operations (Gadget API)
20
- * - BUS: bus glue code, bus abstraction layer
21
- *
22
- * Compile Options
23
- * - STALL_IN: non-empty bulk-in pipes cannot be halted
24
- * if defined mass storage compliance succeeds but with warnings
25
- * => case 4: Hi > Dn
26
- * => case 5: Hi > Di
27
- * => case 8: Hi <> Do
28
- * if undefined usbtest 13 fails
29
- * - TRACE: enable function tracing (depends on DEBUG)
30
- *
31
- * Main Features
32
- * - Chapter 9 & Mass Storage Compliance with Gadget File Storage
33
- * - Chapter 9 Compliance with Gadget Zero (STALL_IN undefined)
34
- * - Normal & LPM support
35
- *
36
- * USBTEST Report
37
- * - OK: 0-12, 13 (STALL_IN defined) & 14
38
- * - Not Supported: 15 & 16 (ISO)
39
- *
40
- * TODO List
41
- * - Suspend & Remote Wakeup
11
+ * Main Features:
12
+ * - Four transfers are supported, usbtest is passed
13
+ * - USB Certification for gadget: CH9 and Mass Storage are passed
14
+ * - Low power mode
15
+ * - USB wakeup
4216 */
4317 #include <linux/delay.h>
4418 #include <linux/device.h>
....@@ -53,6 +27,7 @@
5327 #include <linux/kernel.h>
5428 #include <linux/slab.h>
5529 #include <linux/pm_runtime.h>
30
+#include <linux/pinctrl/consumer.h>
5631 #include <linux/usb/ch9.h>
5732 #include <linux/usb/gadget.h>
5833 #include <linux/usb/otg.h>
....@@ -180,6 +155,7 @@
180155
181156 /**
182157 * hw_port_test_set: writes port test mode (execute without interruption)
158
+ * @ci: the controller
183159 * @mode: new value
184160 *
185161 * This function returns an error code
....@@ -271,7 +247,7 @@
271247 ci->rev = ci_get_revision(ci);
272248
273249 dev_dbg(ci->dev,
274
- "ChipIdea HDRC found, revision: %d, lpm: %d; cap: %p op: %p\n",
250
+ "revision: %d, lpm: %d; cap: %px op: %px\n",
275251 ci->rev, ci->hw_bank.lpm, ci->hw_bank.cap, ci->hw_bank.op);
276252
277253 /* setup lock mode ? */
....@@ -522,8 +498,9 @@
522498 hw_write(ci, OP_USBMODE, USBMODE_SLOM, USBMODE_SLOM);
523499
524500 if (hw_read(ci, OP_USBMODE, USBMODE_CM) != USBMODE_CM_DC) {
525
- pr_err("cannot enter in %s device mode", ci_role(ci)->name);
526
- pr_err("lpm = %i", ci->hw_bank.lpm);
501
+ dev_err(ci->dev, "cannot enter in %s device mode\n",
502
+ ci_role(ci)->name);
503
+ dev_err(ci->dev, "lpm = %i\n", ci->hw_bank.lpm);
527504 return -ENODEV;
528505 }
529506
....@@ -607,6 +584,75 @@
607584 return NOTIFY_DONE;
608585 }
609586
587
+static enum usb_role ci_usb_role_switch_get(struct usb_role_switch *sw)
588
+{
589
+ struct ci_hdrc *ci = usb_role_switch_get_drvdata(sw);
590
+ enum usb_role role;
591
+ unsigned long flags;
592
+
593
+ spin_lock_irqsave(&ci->lock, flags);
594
+ role = ci_role_to_usb_role(ci);
595
+ spin_unlock_irqrestore(&ci->lock, flags);
596
+
597
+ return role;
598
+}
599
+
600
+static int ci_usb_role_switch_set(struct usb_role_switch *sw,
601
+ enum usb_role role)
602
+{
603
+ struct ci_hdrc *ci = usb_role_switch_get_drvdata(sw);
604
+ struct ci_hdrc_cable *cable = NULL;
605
+ enum usb_role current_role = ci_role_to_usb_role(ci);
606
+ enum ci_role ci_role = usb_role_to_ci_role(role);
607
+ unsigned long flags;
608
+
609
+ if ((ci_role != CI_ROLE_END && !ci->roles[ci_role]) ||
610
+ (current_role == role))
611
+ return 0;
612
+
613
+ pm_runtime_get_sync(ci->dev);
614
+ /* Stop current role */
615
+ spin_lock_irqsave(&ci->lock, flags);
616
+ if (current_role == USB_ROLE_DEVICE)
617
+ cable = &ci->platdata->vbus_extcon;
618
+ else if (current_role == USB_ROLE_HOST)
619
+ cable = &ci->platdata->id_extcon;
620
+
621
+ if (cable) {
622
+ cable->changed = true;
623
+ cable->connected = false;
624
+ ci_irq(ci);
625
+ spin_unlock_irqrestore(&ci->lock, flags);
626
+ if (ci->wq && role != USB_ROLE_NONE)
627
+ flush_workqueue(ci->wq);
628
+ spin_lock_irqsave(&ci->lock, flags);
629
+ }
630
+
631
+ cable = NULL;
632
+
633
+ /* Start target role */
634
+ if (role == USB_ROLE_DEVICE)
635
+ cable = &ci->platdata->vbus_extcon;
636
+ else if (role == USB_ROLE_HOST)
637
+ cable = &ci->platdata->id_extcon;
638
+
639
+ if (cable) {
640
+ cable->changed = true;
641
+ cable->connected = true;
642
+ ci_irq(ci);
643
+ }
644
+ spin_unlock_irqrestore(&ci->lock, flags);
645
+ pm_runtime_put_sync(ci->dev);
646
+
647
+ return 0;
648
+}
649
+
650
+static struct usb_role_switch_desc ci_role_switch = {
651
+ .set = ci_usb_role_switch_set,
652
+ .get = ci_usb_role_switch_get,
653
+ .allow_userspace_control = true,
654
+};
655
+
610656 static int ci_get_platdata(struct device *dev,
611657 struct ci_hdrc_platform_data *platdata)
612658 {
....@@ -625,7 +671,7 @@
625671
626672 if (platdata->dr_mode != USB_DR_MODE_PERIPHERAL) {
627673 /* Get the vbus regulator */
628
- platdata->reg_vbus = devm_regulator_get(dev, "vbus");
674
+ platdata->reg_vbus = devm_regulator_get_optional(dev, "vbus");
629675 if (PTR_ERR(platdata->reg_vbus) == -EPROBE_DEFER) {
630676 return -EPROBE_DEFER;
631677 } else if (PTR_ERR(platdata->reg_vbus) == -ENODEV) {
....@@ -732,6 +778,27 @@
732778 else
733779 cable->connected = false;
734780 }
781
+
782
+ if (device_property_read_bool(dev, "usb-role-switch"))
783
+ ci_role_switch.fwnode = dev->fwnode;
784
+
785
+ platdata->pctl = devm_pinctrl_get(dev);
786
+ if (!IS_ERR(platdata->pctl)) {
787
+ struct pinctrl_state *p;
788
+
789
+ p = pinctrl_lookup_state(platdata->pctl, "default");
790
+ if (!IS_ERR(p))
791
+ platdata->pins_default = p;
792
+
793
+ p = pinctrl_lookup_state(platdata->pctl, "host");
794
+ if (!IS_ERR(p))
795
+ platdata->pins_host = p;
796
+
797
+ p = pinctrl_lookup_state(platdata->pctl, "device");
798
+ if (!IS_ERR(p))
799
+ platdata->pins_device = p;
800
+ }
801
+
735802 return 0;
736803 }
737804
....@@ -820,6 +887,33 @@
820887 }
821888 EXPORT_SYMBOL_GPL(ci_hdrc_remove_device);
822889
890
+/**
891
+ * ci_hdrc_query_available_role: get runtime available operation mode
892
+ *
893
+ * The glue layer can get current operation mode (host/peripheral/otg)
894
+ * This function should be called after ci core device has created.
895
+ *
896
+ * @pdev: the platform device of ci core.
897
+ *
898
+ * Return runtime usb_dr_mode.
899
+ */
900
+enum usb_dr_mode ci_hdrc_query_available_role(struct platform_device *pdev)
901
+{
902
+ struct ci_hdrc *ci = platform_get_drvdata(pdev);
903
+
904
+ if (!ci)
905
+ return USB_DR_MODE_UNKNOWN;
906
+ if (ci->roles[CI_ROLE_HOST] && ci->roles[CI_ROLE_GADGET])
907
+ return USB_DR_MODE_OTG;
908
+ else if (ci->roles[CI_ROLE_HOST])
909
+ return USB_DR_MODE_HOST;
910
+ else if (ci->roles[CI_ROLE_GADGET])
911
+ return USB_DR_MODE_PERIPHERAL;
912
+ else
913
+ return USB_DR_MODE_UNKNOWN;
914
+}
915
+EXPORT_SYMBOL_GPL(ci_hdrc_query_available_role);
916
+
823917 static inline void ci_role_destroy(struct ci_hdrc *ci)
824918 {
825919 ci_hdrc_gadget_destroy(ci);
....@@ -892,10 +986,7 @@
892986 &dev_attr_role.attr,
893987 NULL,
894988 };
895
-
896
-static const struct attribute_group ci_attr_group = {
897
- .attrs = ci_attrs,
898
-};
989
+ATTRIBUTE_GROUPS(ci);
899990
900991 static int ci_hdrc_probe(struct platform_device *pdev)
901992 {
....@@ -944,32 +1035,47 @@
9441035 } else if (ci->platdata->usb_phy) {
9451036 ci->usb_phy = ci->platdata->usb_phy;
9461037 } else {
947
- ci->usb_phy = devm_usb_get_phy_by_phandle(dev->parent, "phys",
948
- 0);
1038
+ /* Look for a generic PHY first */
9491039 ci->phy = devm_phy_get(dev->parent, "usb-phy");
9501040
951
- /* Fallback to grabbing any registered USB2 PHY */
952
- if (IS_ERR(ci->usb_phy) &&
953
- PTR_ERR(ci->usb_phy) != -EPROBE_DEFER)
1041
+ if (PTR_ERR(ci->phy) == -EPROBE_DEFER) {
1042
+ ret = -EPROBE_DEFER;
1043
+ goto ulpi_exit;
1044
+ } else if (IS_ERR(ci->phy)) {
1045
+ ci->phy = NULL;
1046
+ }
1047
+
1048
+ /* Look for a legacy USB PHY from device-tree next */
1049
+ if (!ci->phy) {
1050
+ ci->usb_phy = devm_usb_get_phy_by_phandle(dev->parent,
1051
+ "phys", 0);
1052
+
1053
+ if (PTR_ERR(ci->usb_phy) == -EPROBE_DEFER) {
1054
+ ret = -EPROBE_DEFER;
1055
+ goto ulpi_exit;
1056
+ } else if (IS_ERR(ci->usb_phy)) {
1057
+ ci->usb_phy = NULL;
1058
+ }
1059
+ }
1060
+
1061
+ /* Look for any registered legacy USB PHY as last resort */
1062
+ if (!ci->phy && !ci->usb_phy) {
9541063 ci->usb_phy = devm_usb_get_phy(dev->parent,
9551064 USB_PHY_TYPE_USB2);
9561065
957
- /* if both generic PHY and USB PHY layers aren't enabled */
958
- if (PTR_ERR(ci->phy) == -ENOSYS &&
959
- PTR_ERR(ci->usb_phy) == -ENXIO) {
1066
+ if (PTR_ERR(ci->usb_phy) == -EPROBE_DEFER) {
1067
+ ret = -EPROBE_DEFER;
1068
+ goto ulpi_exit;
1069
+ } else if (IS_ERR(ci->usb_phy)) {
1070
+ ci->usb_phy = NULL;
1071
+ }
1072
+ }
1073
+
1074
+ /* No USB PHY was found in the end */
1075
+ if (!ci->phy && !ci->usb_phy) {
9601076 ret = -ENXIO;
9611077 goto ulpi_exit;
9621078 }
963
-
964
- if (IS_ERR(ci->phy) && IS_ERR(ci->usb_phy)) {
965
- ret = -EPROBE_DEFER;
966
- goto ulpi_exit;
967
- }
968
-
969
- if (IS_ERR(ci->phy))
970
- ci->phy = NULL;
971
- else if (IS_ERR(ci->usb_phy))
972
- ci->usb_phy = NULL;
9731079 }
9741080
9751081 ret = ci_usb_phy_init(ci);
....@@ -982,7 +1088,6 @@
9821088
9831089 ci->irq = platform_get_irq(pdev, 0);
9841090 if (ci->irq < 0) {
985
- dev_err(dev, "missing IRQ\n");
9861091 ret = ci->irq;
9871092 goto deinit_phy;
9881093 }
....@@ -1025,6 +1130,16 @@
10251130 }
10261131 }
10271132
1133
+ if (ci_role_switch.fwnode) {
1134
+ ci_role_switch.driver_data = ci;
1135
+ ci->role_switch = usb_role_switch_register(dev,
1136
+ &ci_role_switch);
1137
+ if (IS_ERR(ci->role_switch)) {
1138
+ ret = PTR_ERR(ci->role_switch);
1139
+ goto deinit_otg;
1140
+ }
1141
+ }
1142
+
10281143 if (ci->roles[CI_ROLE_HOST] && ci->roles[CI_ROLE_GADGET]) {
10291144 if (ci->is_otg) {
10301145 ci->role = ci_otg_role(ci);
....@@ -1046,8 +1161,11 @@
10461161
10471162 if (!ci_otg_is_fsm_mode(ci)) {
10481163 /* only update vbus status for peripheral */
1049
- if (ci->role == CI_ROLE_GADGET)
1164
+ if (ci->role == CI_ROLE_GADGET) {
1165
+ /* Pull down DP for possible charger detection */
1166
+ hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
10501167 ci_handle_vbus_change(ci);
1168
+ }
10511169
10521170 ret = ci_role_start(ci, ci->role);
10531171 if (ret) {
....@@ -1080,15 +1198,12 @@
10801198 device_set_wakeup_capable(&pdev->dev, true);
10811199 dbg_create_files(ci);
10821200
1083
- ret = sysfs_create_group(&dev->kobj, &ci_attr_group);
1084
- if (ret)
1085
- goto remove_debug;
1086
-
10871201 return 0;
10881202
1089
-remove_debug:
1090
- dbg_remove_files(ci);
10911203 stop:
1204
+ if (ci->role_switch)
1205
+ usb_role_switch_unregister(ci->role_switch);
1206
+deinit_otg:
10921207 if (ci->is_otg && ci->roles[CI_ROLE_GADGET])
10931208 ci_hdrc_otg_destroy(ci);
10941209 deinit_gadget:
....@@ -1107,6 +1222,9 @@
11071222 {
11081223 struct ci_hdrc *ci = platform_get_drvdata(pdev);
11091224
1225
+ if (ci->role_switch)
1226
+ usb_role_switch_unregister(ci->role_switch);
1227
+
11101228 if (ci->supports_runtime_pm) {
11111229 pm_runtime_get_sync(&pdev->dev);
11121230 pm_runtime_disable(&pdev->dev);
....@@ -1114,7 +1232,6 @@
11141232 }
11151233
11161234 dbg_remove_files(ci);
1117
- sysfs_remove_group(&ci->dev->kobj, &ci_attr_group);
11181235 ci_role_destroy(ci);
11191236 ci_hdrc_enter_lpm(ci, true);
11201237 ci_usb_phy_exit(ci);
....@@ -1317,6 +1434,7 @@
13171434 .driver = {
13181435 .name = "ci_hdrc",
13191436 .pm = &ci_pm_ops,
1437
+ .dev_groups = ci_groups,
13201438 },
13211439 };
13221440