Merge tag 'linux-watchdog-5.2-rc1' of git://www.linux-watchdog.org/linux-watchdog
[linux-2.6-microblaze.git] / drivers / usb / misc / usb251xb.c
index 0468484..4d6ae37 100644 (file)
@@ -12,6 +12,7 @@
 
 #include <linux/delay.h>
 #include <linux/gpio/consumer.h>
+#include <linux/gpio/driver.h>
 #include <linux/i2c.h>
 #include <linux/module.h>
 #include <linux/nls.h>
@@ -222,11 +223,51 @@ static const struct usb251xb_data usb2517i_data = {
        .product_str = "USB2517i",
 };
 
+#ifdef CONFIG_GPIOLIB
+static int usb251xb_check_dev_children(struct device *dev, void *child)
+{
+       if (dev->type == &i2c_adapter_type) {
+               return device_for_each_child(dev, child,
+                                            usb251xb_check_dev_children);
+       }
+
+       return (dev == child);
+}
+
+static int usb251x_check_gpio_chip(struct usb251xb *hub)
+{
+       struct gpio_chip *gc = gpiod_to_chip(hub->gpio_reset);
+       struct i2c_adapter *adap = hub->i2c->adapter;
+       int ret;
+
+       if (!hub->gpio_reset)
+               return 0;
+
+       if (!gc)
+               return -EINVAL;
+
+       ret = usb251xb_check_dev_children(&adap->dev, gc->parent);
+       if (ret) {
+               dev_err(hub->dev, "Reset GPIO chip is at the same i2c-bus\n");
+               return -EINVAL;
+       }
+
+       return 0;
+}
+#else
+static int usb251x_check_gpio_chip(struct usb251xb *hub)
+{
+       return 0;
+}
+#endif
+
 static void usb251xb_reset(struct usb251xb *hub, int state)
 {
        if (!hub->gpio_reset)
                return;
 
+       i2c_lock_bus(hub->i2c->adapter, I2C_LOCK_SEGMENT);
+
        gpiod_set_value_cansleep(hub->gpio_reset, state);
 
        /* wait for hub recovery/stabilization */
@@ -234,6 +275,8 @@ static void usb251xb_reset(struct usb251xb *hub, int state)
                usleep_range(500, 750); /* >=500us at power on */
        else
                usleep_range(1, 10);    /* >=1us at power down */
+
+       i2c_unlock_bus(hub->i2c->adapter, I2C_LOCK_SEGMENT);
 }
 
 static int usb251xb_connect(struct usb251xb *hub)
@@ -331,18 +374,31 @@ out_err:
 }
 
 #ifdef CONFIG_OF
+static void usb251xb_get_ports_field(struct usb251xb *hub,
+                                   const char *prop_name, u8 port_cnt, u8 *fld)
+{
+       struct device *dev = hub->dev;
+       struct property *prop;
+       const __be32 *p;
+       u32 port;
+
+       of_property_for_each_u32(dev->of_node, prop_name, prop, p, port) {
+               if ((port >= 1) && (port <= port_cnt))
+                       *fld |= BIT(port);
+               else
+                       dev_warn(dev, "port %u doesn't exist\n", port);
+       }
+}
+
 static int usb251xb_get_ofdata(struct usb251xb *hub,
                               struct usb251xb_data *data)
 {
        struct device *dev = hub->dev;
        struct device_node *np = dev->of_node;
-       int len, err, i;
-       u32 port, property_u32 = 0;
-       const u32 *cproperty_u32;
+       int len, err;
+       u32 property_u32 = 0;
        const char *cproperty_char;
        char str[USB251XB_STRING_BUFSIZE / 2];
-       struct property *prop;
-       const __be32 *p;
 
        if (!np) {
                dev_err(dev, "failed to get ofdata\n");
@@ -444,46 +500,16 @@ static int usb251xb_get_ofdata(struct usb251xb *hub,
                hub->conf_data3 |= BIT(0);
 
        hub->non_rem_dev = USB251XB_DEF_NON_REMOVABLE_DEVICES;
-       cproperty_u32 = of_get_property(np, "non-removable-ports", &len);
-       if (cproperty_u32 && (len / sizeof(u32)) > 0) {
-               for (i = 0; i < len / sizeof(u32); i++) {
-                       u32 port = be32_to_cpu(cproperty_u32[i]);
-
-                       if ((port >= 1) && (port <= data->port_cnt))
-                               hub->non_rem_dev |= BIT(port);
-                       else
-                               dev_warn(dev, "NRD port %u doesn't exist\n",
-                                       port);
-               }
-       }
+       usb251xb_get_ports_field(hub, "non-removable-ports", data->port_cnt,
+                                &hub->non_rem_dev);
 
        hub->port_disable_sp = USB251XB_DEF_PORT_DISABLE_SELF;
-       cproperty_u32 = of_get_property(np, "sp-disabled-ports", &len);
-       if (cproperty_u32 && (len / sizeof(u32)) > 0) {
-               for (i = 0; i < len / sizeof(u32); i++) {
-                       u32 port = be32_to_cpu(cproperty_u32[i]);
-
-                       if ((port >= 1) && (port <= data->port_cnt))
-                               hub->port_disable_sp |= BIT(port);
-                       else
-                               dev_warn(dev, "PDS port %u doesn't exist\n",
-                                       port);
-               }
-       }
+       usb251xb_get_ports_field(hub, "sp-disabled-ports", data->port_cnt,
+                                &hub->port_disable_sp);
 
        hub->port_disable_bp = USB251XB_DEF_PORT_DISABLE_BUS;
-       cproperty_u32 = of_get_property(np, "bp-disabled-ports", &len);
-       if (cproperty_u32 && (len / sizeof(u32)) > 0) {
-               for (i = 0; i < len / sizeof(u32); i++) {
-                       u32 port = be32_to_cpu(cproperty_u32[i]);
-
-                       if ((port >= 1) && (port <= data->port_cnt))
-                               hub->port_disable_bp |= BIT(port);
-                       else
-                               dev_warn(dev, "PDB port %u doesn't exist\n",
-                                       port);
-               }
-       }
+       usb251xb_get_ports_field(hub, "bp-disabled-ports", data->port_cnt,
+                                &hub->port_disable_bp);
 
        hub->max_power_sp = USB251XB_DEF_MAX_POWER_SELF;
        if (!of_property_read_u32(np, "sp-max-total-current-microamp",
@@ -546,10 +572,10 @@ static int usb251xb_get_ofdata(struct usb251xb *hub,
         * register controls the USB DP/DM signal swapping for each port.
         */
        hub->port_swap = USB251XB_DEF_PORT_SWAP;
-       of_property_for_each_u32(np, "swap-dx-lanes", prop, p, port) {
-               if (port <= data->port_cnt)
-                       hub->port_swap |= BIT(port);
-       }
+       usb251xb_get_ports_field(hub, "swap-dx-lanes", data->port_cnt,
+                                &hub->port_swap);
+       if (of_get_property(np, "swap-us-lanes", NULL))
+               hub->port_swap |= BIT(0);
 
        /* The following parameters are currently not exposed to devicetree, but
         * may be as soon as needed.
@@ -621,6 +647,25 @@ static int usb251xb_probe(struct usb251xb *hub)
                }
        }
 
+       /*
+        * usb251x SMBus-slave SCL lane is muxed with CFG_SEL0 pin. So if anyone
+        * tries to work with the bus at the moment the hub reset is released,
+        * it may cause an invalid config being latched by usb251x. Particularly
+        * one of the config modes makes the hub loading a default registers
+        * value without SMBus-slave interface activation. If the hub
+        * accidentally gets this mode, this will cause the driver SMBus-
+        * functions failure. Normally we could just lock the SMBus-segment the
+        * hub i2c-interface resides for the device-specific reset timing. But
+        * the GPIO controller, which is used to handle the hub reset, might be
+        * placed at the same i2c-bus segment. In this case an error should be
+        * returned since we can't safely use the GPIO controller to clear the
+        * reset state (it may affect the hub configuration) and we can't lock
+        * the i2c-bus segment (it will cause a deadlock).
+        */
+       err = usb251x_check_gpio_chip(hub);
+       if (err)
+               return err;
+
        err = usb251xb_connect(hub);
        if (err) {
                dev_err(dev, "Failed to connect hub (%d)\n", err);