hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
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);
....@@ -872,8 +966,15 @@
872966 strlen(ci->roles[role]->name)))
873967 break;
874968
875
- if (role == CI_ROLE_END || role == ci->role)
969
+ if (role == CI_ROLE_END)
876970 return -EINVAL;
971
+
972
+ mutex_lock(&ci->mutex);
973
+
974
+ if (role == ci->role) {
975
+ mutex_unlock(&ci->mutex);
976
+ return n;
977
+ }
877978
878979 pm_runtime_get_sync(dev);
879980 disable_irq(ci->irq);
....@@ -883,6 +984,7 @@
883984 ci_handle_vbus_change(ci);
884985 enable_irq(ci->irq);
885986 pm_runtime_put_sync(dev);
987
+ mutex_unlock(&ci->mutex);
886988
887989 return (ret == 0) ? n : ret;
888990 }
....@@ -892,10 +994,7 @@
892994 &dev_attr_role.attr,
893995 NULL,
894996 };
895
-
896
-static const struct attribute_group ci_attr_group = {
897
- .attrs = ci_attrs,
898
-};
997
+ATTRIBUTE_GROUPS(ci);
899998
900999 static int ci_hdrc_probe(struct platform_device *pdev)
9011000 {
....@@ -921,6 +1020,7 @@
9211020 return -ENOMEM;
9221021
9231022 spin_lock_init(&ci->lock);
1023
+ mutex_init(&ci->mutex);
9241024 ci->dev = dev;
9251025 ci->platdata = dev_get_platdata(dev);
9261026 ci->imx28_write_fix = !!(ci->platdata->flags &
....@@ -944,45 +1044,59 @@
9441044 } else if (ci->platdata->usb_phy) {
9451045 ci->usb_phy = ci->platdata->usb_phy;
9461046 } else {
947
- ci->usb_phy = devm_usb_get_phy_by_phandle(dev->parent, "phys",
948
- 0);
1047
+ /* Look for a generic PHY first */
9491048 ci->phy = devm_phy_get(dev->parent, "usb-phy");
9501049
951
- /* Fallback to grabbing any registered USB2 PHY */
952
- if (IS_ERR(ci->usb_phy) &&
953
- PTR_ERR(ci->usb_phy) != -EPROBE_DEFER)
1050
+ if (PTR_ERR(ci->phy) == -EPROBE_DEFER) {
1051
+ ret = -EPROBE_DEFER;
1052
+ goto ulpi_exit;
1053
+ } else if (IS_ERR(ci->phy)) {
1054
+ ci->phy = NULL;
1055
+ }
1056
+
1057
+ /* Look for a legacy USB PHY from device-tree next */
1058
+ if (!ci->phy) {
1059
+ ci->usb_phy = devm_usb_get_phy_by_phandle(dev->parent,
1060
+ "phys", 0);
1061
+
1062
+ if (PTR_ERR(ci->usb_phy) == -EPROBE_DEFER) {
1063
+ ret = -EPROBE_DEFER;
1064
+ goto ulpi_exit;
1065
+ } else if (IS_ERR(ci->usb_phy)) {
1066
+ ci->usb_phy = NULL;
1067
+ }
1068
+ }
1069
+
1070
+ /* Look for any registered legacy USB PHY as last resort */
1071
+ if (!ci->phy && !ci->usb_phy) {
9541072 ci->usb_phy = devm_usb_get_phy(dev->parent,
9551073 USB_PHY_TYPE_USB2);
9561074
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) {
1075
+ if (PTR_ERR(ci->usb_phy) == -EPROBE_DEFER) {
1076
+ ret = -EPROBE_DEFER;
1077
+ goto ulpi_exit;
1078
+ } else if (IS_ERR(ci->usb_phy)) {
1079
+ ci->usb_phy = NULL;
1080
+ }
1081
+ }
1082
+
1083
+ /* No USB PHY was found in the end */
1084
+ if (!ci->phy && !ci->usb_phy) {
9601085 ret = -ENXIO;
9611086 goto ulpi_exit;
9621087 }
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;
9731088 }
9741089
9751090 ret = ci_usb_phy_init(ci);
9761091 if (ret) {
9771092 dev_err(dev, "unable to init phy: %d\n", ret);
978
- return ret;
1093
+ goto ulpi_exit;
9791094 }
9801095
9811096 ci->hw_bank.phys = res->start;
9821097
9831098 ci->irq = platform_get_irq(pdev, 0);
9841099 if (ci->irq < 0) {
985
- dev_err(dev, "missing IRQ\n");
9861100 ret = ci->irq;
9871101 goto deinit_phy;
9881102 }
....@@ -1025,6 +1139,16 @@
10251139 }
10261140 }
10271141
1142
+ if (ci_role_switch.fwnode) {
1143
+ ci_role_switch.driver_data = ci;
1144
+ ci->role_switch = usb_role_switch_register(dev,
1145
+ &ci_role_switch);
1146
+ if (IS_ERR(ci->role_switch)) {
1147
+ ret = PTR_ERR(ci->role_switch);
1148
+ goto deinit_otg;
1149
+ }
1150
+ }
1151
+
10281152 if (ci->roles[CI_ROLE_HOST] && ci->roles[CI_ROLE_GADGET]) {
10291153 if (ci->is_otg) {
10301154 ci->role = ci_otg_role(ci);
....@@ -1046,8 +1170,11 @@
10461170
10471171 if (!ci_otg_is_fsm_mode(ci)) {
10481172 /* only update vbus status for peripheral */
1049
- if (ci->role == CI_ROLE_GADGET)
1173
+ if (ci->role == CI_ROLE_GADGET) {
1174
+ /* Pull down DP for possible charger detection */
1175
+ hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
10501176 ci_handle_vbus_change(ci);
1177
+ }
10511178
10521179 ret = ci_role_start(ci, ci->role);
10531180 if (ret) {
....@@ -1080,15 +1207,12 @@
10801207 device_set_wakeup_capable(&pdev->dev, true);
10811208 dbg_create_files(ci);
10821209
1083
- ret = sysfs_create_group(&dev->kobj, &ci_attr_group);
1084
- if (ret)
1085
- goto remove_debug;
1086
-
10871210 return 0;
10881211
1089
-remove_debug:
1090
- dbg_remove_files(ci);
10911212 stop:
1213
+ if (ci->role_switch)
1214
+ usb_role_switch_unregister(ci->role_switch);
1215
+deinit_otg:
10921216 if (ci->is_otg && ci->roles[CI_ROLE_GADGET])
10931217 ci_hdrc_otg_destroy(ci);
10941218 deinit_gadget:
....@@ -1107,6 +1231,9 @@
11071231 {
11081232 struct ci_hdrc *ci = platform_get_drvdata(pdev);
11091233
1234
+ if (ci->role_switch)
1235
+ usb_role_switch_unregister(ci->role_switch);
1236
+
11101237 if (ci->supports_runtime_pm) {
11111238 pm_runtime_get_sync(&pdev->dev);
11121239 pm_runtime_disable(&pdev->dev);
....@@ -1114,7 +1241,6 @@
11141241 }
11151242
11161243 dbg_remove_files(ci);
1117
- sysfs_remove_group(&ci->dev->kobj, &ci_attr_group);
11181244 ci_role_destroy(ci);
11191245 ci_hdrc_enter_lpm(ci, true);
11201246 ci_usb_phy_exit(ci);
....@@ -1317,6 +1443,7 @@
13171443 .driver = {
13181444 .name = "ci_hdrc",
13191445 .pm = &ci_pm_ops,
1446
+ .dev_groups = ci_groups,
13201447 },
13211448 };
13221449