hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/drivers/usb/gadget/udc/pch_udc.c
....@@ -7,16 +7,15 @@
77 #include <linux/module.h>
88 #include <linux/pci.h>
99 #include <linux/delay.h>
10
+#include <linux/dmi.h>
1011 #include <linux/errno.h>
12
+#include <linux/gpio/consumer.h>
13
+#include <linux/gpio/machine.h>
1114 #include <linux/list.h>
1215 #include <linux/interrupt.h>
1316 #include <linux/usb/ch9.h>
1417 #include <linux/usb/gadget.h>
15
-#include <linux/gpio.h>
1618 #include <linux/irq.h>
17
-
18
-/* GPIO port for VBUS detecting */
19
-static int vbus_gpio_port = -1; /* GPIO port number (-1:Not used) */
2019
2120 #define PCH_VBUS_PERIOD 3000 /* VBUS polling period (msec) */
2221 #define PCH_VBUS_INTERVAL 10 /* VBUS polling interval (msec) */
....@@ -229,8 +228,7 @@
229228 * for control data
230229 * @status: Status
231230 * @reserved: Reserved
232
- * @data12: First setup word
233
- * @data34: Second setup word
231
+ * @request: Control Request
234232 */
235233 struct pch_udc_stp_dma_desc {
236234 u32 status;
....@@ -302,13 +300,13 @@
302300 /**
303301 * struct pch_vbus_gpio_data - Structure holding GPIO informaton
304302 * for detecting VBUS
305
- * @port: gpio port number
303
+ * @port: gpio descriptor for the VBUS GPIO
306304 * @intr: gpio interrupt number
307
- * @irq_work_fall Structure for WorkQueue
308
- * @irq_work_rise Structure for WorkQueue
305
+ * @irq_work_fall: Structure for WorkQueue
306
+ * @irq_work_rise: Structure for WorkQueue
309307 */
310308 struct pch_vbus_gpio_data {
311
- int port;
309
+ struct gpio_desc *port;
312310 int intr;
313311 struct work_struct irq_work_fall;
314312 struct work_struct irq_work_rise;
....@@ -475,7 +473,7 @@
475473 * pch_udc_write_csr() - Write the command and status registers.
476474 * @dev: Reference to pch_udc_dev structure
477475 * @val: value to be written to CSR register
478
- * @addr: address of CSR register
476
+ * @ep: end-point number
479477 */
480478 static void pch_udc_write_csr(struct pch_udc_dev *dev, unsigned long val,
481479 unsigned int ep)
....@@ -490,7 +488,7 @@
490488 /**
491489 * pch_udc_read_csr() - Read the command and status registers.
492490 * @dev: Reference to pch_udc_dev structure
493
- * @addr: address of CSR register
491
+ * @ep: end-point number
494492 *
495493 * Return codes: content of CSR register
496494 */
....@@ -660,6 +658,7 @@
660658 * pch_udc_ep_set_bufsz() - Set the maximum packet size for the endpoint
661659 * @ep: Reference to structure of type pch_udc_ep_regs
662660 * @buf_size: The buffer word size
661
+ * @ep_in: EP is IN
663662 */
664663 static void pch_udc_ep_set_bufsz(struct pch_udc_ep *ep,
665664 u32 buf_size, u32 ep_in)
....@@ -972,7 +971,8 @@
972971
973972 /**
974973 * pch_udc_ep_enable() - This api enables endpoint
975
- * @regs: Reference to structure pch_udc_ep_regs
974
+ * @ep: reference to structure of type pch_udc_ep_regs
975
+ * @cfg: current configuration information
976976 * @desc: endpoint descriptor
977977 */
978978 static void pch_udc_ep_enable(struct pch_udc_ep *ep,
....@@ -1008,7 +1008,7 @@
10081008
10091009 /**
10101010 * pch_udc_ep_disable() - This api disables endpoint
1011
- * @regs: Reference to structure pch_udc_ep_regs
1011
+ * @ep: reference to structure of type pch_udc_ep_regs
10121012 */
10131013 static void pch_udc_ep_disable(struct pch_udc_ep *ep)
10141014 {
....@@ -1028,7 +1028,7 @@
10281028
10291029 /**
10301030 * pch_udc_wait_ep_stall() - Wait EP stall.
1031
- * @dev: Reference to pch_udc_dev structure
1031
+ * @ep: reference to structure of type pch_udc_ep_regs
10321032 */
10331033 static void pch_udc_wait_ep_stall(struct pch_udc_ep *ep)
10341034 {
....@@ -1262,7 +1262,7 @@
12621262 int vbus = 0;
12631263
12641264 if (dev->vbus_gpio.port)
1265
- vbus = gpio_get_value(dev->vbus_gpio.port) ? 1 : 0;
1265
+ vbus = gpiod_get_value(dev->vbus_gpio.port) ? 1 : 0;
12661266 else
12671267 vbus = -1;
12681268
....@@ -1338,9 +1338,9 @@
13381338 }
13391339
13401340 /**
1341
- * pch_vbus_gpio_irq() - IRQ handler for GPIO intrerrupt for changing VBUS
1341
+ * pch_vbus_gpio_irq() - IRQ handler for GPIO interrupt for changing VBUS
13421342 * @irq: Interrupt request number
1343
- * @dev: Reference to the device structure
1343
+ * @data: Reference to the device structure
13441344 *
13451345 * Return codes:
13461346 * 0: Success
....@@ -1361,45 +1361,75 @@
13611361 return IRQ_HANDLED;
13621362 }
13631363
1364
+static struct gpiod_lookup_table minnowboard_udc_gpios = {
1365
+ .dev_id = "0000:02:02.4",
1366
+ .table = {
1367
+ GPIO_LOOKUP("sch_gpio.33158", 12, NULL, GPIO_ACTIVE_HIGH),
1368
+ {}
1369
+ },
1370
+};
1371
+
1372
+static const struct dmi_system_id pch_udc_gpio_dmi_table[] = {
1373
+ {
1374
+ .ident = "MinnowBoard",
1375
+ .matches = {
1376
+ DMI_MATCH(DMI_BOARD_NAME, "MinnowBoard"),
1377
+ },
1378
+ .driver_data = &minnowboard_udc_gpios,
1379
+ },
1380
+ { }
1381
+};
1382
+
1383
+static void pch_vbus_gpio_remove_table(void *table)
1384
+{
1385
+ gpiod_remove_lookup_table(table);
1386
+}
1387
+
1388
+static int pch_vbus_gpio_add_table(struct pch_udc_dev *dev)
1389
+{
1390
+ struct device *d = &dev->pdev->dev;
1391
+ const struct dmi_system_id *dmi;
1392
+
1393
+ dmi = dmi_first_match(pch_udc_gpio_dmi_table);
1394
+ if (!dmi)
1395
+ return 0;
1396
+
1397
+ gpiod_add_lookup_table(dmi->driver_data);
1398
+ return devm_add_action_or_reset(d, pch_vbus_gpio_remove_table, dmi->driver_data);
1399
+}
1400
+
13641401 /**
13651402 * pch_vbus_gpio_init() - This API initializes GPIO port detecting VBUS.
1366
- * @dev: Reference to the driver structure
1367
- * @vbus_gpio Number of GPIO port to detect gpio
1403
+ * @dev: Reference to the driver structure
13681404 *
13691405 * Return codes:
13701406 * 0: Success
13711407 * -EINVAL: GPIO port is invalid or can't be initialized.
13721408 */
1373
-static int pch_vbus_gpio_init(struct pch_udc_dev *dev, int vbus_gpio_port)
1409
+static int pch_vbus_gpio_init(struct pch_udc_dev *dev)
13741410 {
1411
+ struct device *d = &dev->pdev->dev;
13751412 int err;
13761413 int irq_num = 0;
1414
+ struct gpio_desc *gpiod;
13771415
1378
- dev->vbus_gpio.port = 0;
1416
+ dev->vbus_gpio.port = NULL;
13791417 dev->vbus_gpio.intr = 0;
13801418
1381
- if (vbus_gpio_port <= -1)
1382
- return -EINVAL;
1419
+ err = pch_vbus_gpio_add_table(dev);
1420
+ if (err)
1421
+ return err;
13831422
1384
- err = gpio_is_valid(vbus_gpio_port);
1385
- if (!err) {
1386
- pr_err("%s: gpio port %d is invalid\n",
1387
- __func__, vbus_gpio_port);
1388
- return -EINVAL;
1389
- }
1423
+ /* Retrieve the GPIO line from the USB gadget device */
1424
+ gpiod = devm_gpiod_get_optional(d, NULL, GPIOD_IN);
1425
+ if (IS_ERR(gpiod))
1426
+ return PTR_ERR(gpiod);
1427
+ gpiod_set_consumer_name(gpiod, "pch_vbus");
13901428
1391
- err = gpio_request(vbus_gpio_port, "pch_vbus");
1392
- if (err) {
1393
- pr_err("%s: can't request gpio port %d, err: %d\n",
1394
- __func__, vbus_gpio_port, err);
1395
- return -EINVAL;
1396
- }
1397
-
1398
- dev->vbus_gpio.port = vbus_gpio_port;
1399
- gpio_direction_input(vbus_gpio_port);
1429
+ dev->vbus_gpio.port = gpiod;
14001430 INIT_WORK(&dev->vbus_gpio.irq_work_fall, pch_vbus_gpio_work_fall);
14011431
1402
- irq_num = gpio_to_irq(vbus_gpio_port);
1432
+ irq_num = gpiod_to_irq(gpiod);
14031433 if (irq_num > 0) {
14041434 irq_set_irq_type(irq_num, IRQ_TYPE_EDGE_BOTH);
14051435 err = request_irq(irq_num, pch_vbus_gpio_irq, 0,
....@@ -1425,9 +1455,6 @@
14251455 {
14261456 if (dev->vbus_gpio.intr)
14271457 free_irq(dev->vbus_gpio.intr, dev);
1428
-
1429
- if (dev->vbus_gpio.port)
1430
- gpio_free(dev->vbus_gpio.port);
14311458 }
14321459
14331460 /**
....@@ -1508,8 +1535,8 @@
15081535 /**
15091536 * pch_udc_free_dma_chain() - This function frees the DMA chain created
15101537 * for the request
1511
- * @dev Reference to the driver structure
1512
- * @req Reference to the request to be freed
1538
+ * @dev: Reference to the driver structure
1539
+ * @req: Reference to the request to be freed
15131540 *
15141541 * Return codes:
15151542 * 0: Success
....@@ -1716,7 +1743,7 @@
17161743 /**
17171744 * pch_udc_pcd_ep_disable() - This API disables endpoint and is called
17181745 * from gadget driver
1719
- * @usbep Reference to the USB endpoint structure
1746
+ * @usbep: Reference to the USB endpoint structure
17201747 *
17211748 * Return codes:
17221749 * 0: Success
....@@ -2005,7 +2032,6 @@
20052032 * pch_udc_pcd_set_wedge() - This function Sets or clear the endpoint
20062033 * halt feature
20072034 * @usbep: Reference to the USB endpoint structure
2008
- * @halt: Specifies whether to set or clear the feature
20092035 *
20102036 * Return codes:
20112037 * 0: Success
....@@ -2767,7 +2793,7 @@
27672793 /**
27682794 * pch_udc_isr() - This function handles interrupts from the PCH USB Device
27692795 * @irq: Interrupt request number
2770
- * @dev: Reference to the device structure
2796
+ * @pdev: Reference to the device structure
27712797 */
27722798 static irqreturn_t pch_udc_isr(int irq, void *pdev)
27732799 {
....@@ -2905,19 +2931,25 @@
29052931 * @dev: Reference to the driver structure
29062932 *
29072933 * Return codes:
2908
- * 0: Success
2934
+ * 0: Success
2935
+ * -%ERRNO: All kind of errors when retrieving VBUS GPIO
29092936 */
29102937 static int pch_udc_pcd_init(struct pch_udc_dev *dev)
29112938 {
2939
+ int ret;
2940
+
29122941 pch_udc_init(dev);
29132942 pch_udc_pcd_reinit(dev);
2914
- pch_vbus_gpio_init(dev, vbus_gpio_port);
2915
- return 0;
2943
+
2944
+ ret = pch_vbus_gpio_init(dev);
2945
+ if (ret)
2946
+ pch_udc_exit(dev);
2947
+ return ret;
29162948 }
29172949
29182950 /**
29192951 * init_dma_pools() - create dma pools during initialization
2920
- * @pdev: reference to struct pci_dev
2952
+ * @dev: reference to struct pci_dev
29212953 */
29222954 static int init_dma_pools(struct pch_udc_dev *dev)
29232955 {
....@@ -3062,8 +3094,7 @@
30623094 #ifdef CONFIG_PM_SLEEP
30633095 static int pch_udc_suspend(struct device *d)
30643096 {
3065
- struct pci_dev *pdev = to_pci_dev(d);
3066
- struct pch_udc_dev *dev = pci_get_drvdata(pdev);
3097
+ struct pch_udc_dev *dev = dev_get_drvdata(d);
30673098
30683099 pch_udc_disable_interrupts(dev, UDC_DEVINT_MSK);
30693100 pch_udc_disable_ep_interrupts(dev, UDC_EPINT_MSK_DISABLE_ALL);
....@@ -3099,6 +3130,7 @@
30993130 if (retval)
31003131 return retval;
31013132
3133
+ dev->pdev = pdev;
31023134 pci_set_drvdata(pdev, dev);
31033135
31043136 /* Determine BAR based on PCI ID */
....@@ -3115,8 +3147,9 @@
31153147 dev->base_addr = pcim_iomap_table(pdev)[bar];
31163148
31173149 /* initialize the hardware */
3118
- if (pch_udc_pcd_init(dev))
3119
- return -ENODEV;
3150
+ retval = pch_udc_pcd_init(dev);
3151
+ if (retval)
3152
+ return retval;
31203153
31213154 pci_enable_msi(pdev);
31223155
....@@ -3133,7 +3166,6 @@
31333166
31343167 /* device struct setup */
31353168 spin_lock_init(&dev->lock);
3136
- dev->pdev = pdev;
31373169 dev->gadget.ops = &pch_udc_ops;
31383170
31393171 retval = init_dma_pools(dev);