Merge branch 'for-5.10' of https://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[linux-2.6-microblaze.git] / drivers / spi / spi.c
index 1564914..cd3c395 100644 (file)
@@ -821,18 +821,16 @@ static void spi_set_cs(struct spi_device *spi, bool enable)
                enable = !enable;
 
        if (spi->cs_gpiod || gpio_is_valid(spi->cs_gpio)) {
-               /*
-                * Honour the SPI_NO_CS flag and invert the enable line, as
-                * active low is default for SPI. Execution paths that handle
-                * polarity inversion in gpiolib (such as device tree) will
-                * enforce active high using the SPI_CS_HIGH resulting in a
-                * double inversion through the code above.
-                */
                if (!(spi->mode & SPI_NO_CS)) {
                        if (spi->cs_gpiod)
+                               /* polarity handled by gpiolib */
                                gpiod_set_value_cansleep(spi->cs_gpiod,
-                                                        !enable);
+                                                        enable1);
                        else
+                               /*
+                                * invert the enable line, as active low is
+                                * default for SPI.
+                                */
                                gpio_set_value_cansleep(spi->cs_gpio, !enable);
                }
                /* Some SPI masters need both GPIO CS & slave_select */
@@ -2001,15 +1999,6 @@ static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi,
        }
        spi->chip_select = value;
 
-       /*
-        * For descriptors associated with the device, polarity inversion is
-        * handled in the gpiolib, so all gpio chip selects are "active high"
-        * in the logical sense, the gpiolib will invert the line if need be.
-        */
-       if ((ctlr->use_gpio_descriptors) && ctlr->cs_gpiods &&
-           ctlr->cs_gpiods[spi->chip_select])
-               spi->mode |= SPI_CS_HIGH;
-
        /* Device speed */
        if (!of_property_read_u32(nc, "spi-max-frequency", &value))
                spi->max_speed_hz = value;
@@ -2462,6 +2451,49 @@ struct spi_controller *__spi_alloc_controller(struct device *dev,
 }
 EXPORT_SYMBOL_GPL(__spi_alloc_controller);
 
+static void devm_spi_release_controller(struct device *dev, void *ctlr)
+{
+       spi_controller_put(*(struct spi_controller **)ctlr);
+}
+
+/**
+ * __devm_spi_alloc_controller - resource-managed __spi_alloc_controller()
+ * @dev: physical device of SPI controller
+ * @size: how much zeroed driver-private data to allocate
+ * @slave: whether to allocate an SPI master (false) or SPI slave (true)
+ * Context: can sleep
+ *
+ * Allocate an SPI controller and automatically release a reference on it
+ * when @dev is unbound from its driver.  Drivers are thus relieved from
+ * having to call spi_controller_put().
+ *
+ * The arguments to this function are identical to __spi_alloc_controller().
+ *
+ * Return: the SPI controller structure on success, else NULL.
+ */
+struct spi_controller *__devm_spi_alloc_controller(struct device *dev,
+                                                  unsigned int size,
+                                                  bool slave)
+{
+       struct spi_controller **ptr, *ctlr;
+
+       ptr = devres_alloc(devm_spi_release_controller, sizeof(*ptr),
+                          GFP_KERNEL);
+       if (!ptr)
+               return NULL;
+
+       ctlr = __spi_alloc_controller(dev, size, slave);
+       if (ctlr) {
+               *ptr = ctlr;
+               devres_add(dev, ptr);
+       } else {
+               devres_free(ptr);
+       }
+
+       return ctlr;
+}
+EXPORT_SYMBOL_GPL(__devm_spi_alloc_controller);
+
 #ifdef CONFIG_OF
 static int of_spi_get_gpio_numbers(struct spi_controller *ctlr)
 {
@@ -2798,6 +2830,11 @@ int devm_spi_register_controller(struct device *dev,
 }
 EXPORT_SYMBOL_GPL(devm_spi_register_controller);
 
+static int devm_spi_match_controller(struct device *dev, void *res, void *ctlr)
+{
+       return *(struct spi_controller **)res == ctlr;
+}
+
 static int __unregister(struct device *dev, void *null)
 {
        spi_unregister_device(to_spi_device(dev));
@@ -2839,7 +2876,15 @@ void spi_unregister_controller(struct spi_controller *ctlr)
        list_del(&ctlr->list);
        mutex_unlock(&board_lock);
 
-       device_unregister(&ctlr->dev);
+       device_del(&ctlr->dev);
+
+       /* Release the last reference on the controller if its driver
+        * has not yet been converted to devm_spi_alloc_master/slave().
+        */
+       if (!devres_find(ctlr->dev.parent, devm_spi_release_controller,
+                        devm_spi_match_controller, ctlr))
+               put_device(&ctlr->dev);
+
        /* free bus id */
        mutex_lock(&board_lock);
        if (found == ctlr)
@@ -3336,12 +3381,15 @@ int spi_setup(struct spi_device *spi)
        if (!spi->max_speed_hz)
                spi->max_speed_hz = spi->controller->max_speed_hz;
 
+       mutex_lock(&spi->controller->io_mutex);
+
        if (spi->controller->setup)
                status = spi->controller->setup(spi);
 
        if (spi->controller->auto_runtime_pm && spi->controller->set_cs) {
                status = pm_runtime_get_sync(spi->controller->dev.parent);
                if (status < 0) {
+                       mutex_unlock(&spi->controller->io_mutex);
                        pm_runtime_put_noidle(spi->controller->dev.parent);
                        dev_err(&spi->controller->dev, "Failed to power device: %d\n",
                                status);
@@ -3363,6 +3411,8 @@ int spi_setup(struct spi_device *spi)
                spi_set_cs(spi, false);
        }
 
+       mutex_unlock(&spi->controller->io_mutex);
+
        if (spi->rt && !spi->controller->rt) {
                spi->controller->rt = true;
                spi_set_thread_rt(spi->controller);