Merge tag 'devicetree-fixes-for-6.2-3' of git://git.kernel.org/pub/scm/linux/kernel...
[sfrench/cifs-2.6.git] / drivers / spi / spi.c
index 94f60f7561d4c6c15ca061665e994018a09c3f93..7bc14fb309a69970ea92902e4f1ec88b32a41e8c 100644 (file)
@@ -395,7 +395,7 @@ static int spi_match_device(struct device *dev, struct device_driver *drv)
        return strcmp(spi->modalias, drv->name) == 0;
 }
 
-static int spi_uevent(struct device *dev, struct kobj_uevent_env *env)
+static int spi_uevent(const struct device *dev, struct kobj_uevent_env *env)
 {
        const struct spi_device         *spi = to_spi_device(dev);
        int rc;
@@ -604,7 +604,7 @@ static void spi_dev_set_name(struct spi_device *spi)
        }
 
        dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev),
-                    spi->chip_select);
+                    spi_get_chipselect(spi, 0));
 }
 
 static int spi_dev_check(struct device *dev, void *data)
@@ -613,7 +613,7 @@ static int spi_dev_check(struct device *dev, void *data)
        struct spi_device *new_spi = data;
 
        if (spi->controller == new_spi->controller &&
-           spi->chip_select == new_spi->chip_select)
+           spi_get_chipselect(spi, 0) == spi_get_chipselect(new_spi, 0))
                return -EBUSY;
        return 0;
 }
@@ -638,7 +638,7 @@ static int __spi_add_device(struct spi_device *spi)
        status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check);
        if (status) {
                dev_err(dev, "chipselect %d already in use\n",
-                               spi->chip_select);
+                               spi_get_chipselect(spi, 0));
                return status;
        }
 
@@ -649,7 +649,7 @@ static int __spi_add_device(struct spi_device *spi)
        }
 
        if (ctlr->cs_gpiods)
-               spi->cs_gpiod = ctlr->cs_gpiods[spi->chip_select];
+               spi_set_csgpiod(spi, 0, ctlr->cs_gpiods[spi_get_chipselect(spi, 0)]);
 
        /*
         * Drivers may modify this initial i/o setup, but will
@@ -692,8 +692,8 @@ int spi_add_device(struct spi_device *spi)
        int status;
 
        /* Chipselects are numbered 0..max; validate. */
-       if (spi->chip_select >= ctlr->num_chipselect) {
-               dev_err(dev, "cs%d >= max %d\n", spi->chip_select,
+       if (spi_get_chipselect(spi, 0) >= ctlr->num_chipselect) {
+               dev_err(dev, "cs%d >= max %d\n", spi_get_chipselect(spi, 0),
                        ctlr->num_chipselect);
                return -EINVAL;
        }
@@ -714,8 +714,8 @@ static int spi_add_device_locked(struct spi_device *spi)
        struct device *dev = ctlr->dev.parent;
 
        /* Chipselects are numbered 0..max; validate. */
-       if (spi->chip_select >= ctlr->num_chipselect) {
-               dev_err(dev, "cs%d >= max %d\n", spi->chip_select,
+       if (spi_get_chipselect(spi, 0) >= ctlr->num_chipselect) {
+               dev_err(dev, "cs%d >= max %d\n", spi_get_chipselect(spi, 0),
                        ctlr->num_chipselect);
                return -EINVAL;
        }
@@ -761,7 +761,7 @@ struct spi_device *spi_new_device(struct spi_controller *ctlr,
 
        WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
 
-       proxy->chip_select = chip->chip_select;
+       spi_set_chipselect(proxy, 0, chip->chip_select);
        proxy->max_speed_hz = chip->max_speed_hz;
        proxy->mode = chip->mode;
        proxy->irq = chip->irq;
@@ -970,24 +970,23 @@ static void spi_set_cs(struct spi_device *spi, bool enable, bool force)
         * Avoid calling into the driver (or doing delays) if the chip select
         * isn't actually changing from the last time this was called.
         */
-       if (!force && ((enable && spi->controller->last_cs == spi->chip_select) ||
-                               (!enable && spi->controller->last_cs != spi->chip_select)) &&
+       if (!force && ((enable && spi->controller->last_cs == spi_get_chipselect(spi, 0)) ||
+                      (!enable && spi->controller->last_cs != spi_get_chipselect(spi, 0))) &&
            (spi->controller->last_cs_mode_high == (spi->mode & SPI_CS_HIGH)))
                return;
 
        trace_spi_set_cs(spi, activate);
 
-       spi->controller->last_cs = enable ? spi->chip_select : -1;
+       spi->controller->last_cs = enable ? spi_get_chipselect(spi, 0) : -1;
        spi->controller->last_cs_mode_high = spi->mode & SPI_CS_HIGH;
 
-       if ((spi->cs_gpiod || !spi->controller->set_cs_timing) && !activate) {
+       if ((spi_get_csgpiod(spi, 0) || !spi->controller->set_cs_timing) && !activate)
                spi_delay_exec(&spi->cs_hold, NULL);
-       }
 
        if (spi->mode & SPI_CS_HIGH)
                enable = !enable;
 
-       if (spi->cs_gpiod) {
+       if (spi_get_csgpiod(spi, 0)) {
                if (!(spi->mode & SPI_NO_CS)) {
                        /*
                         * Historically ACPI has no means of the GPIO polarity and
@@ -1000,10 +999,10 @@ static void spi_set_cs(struct spi_device *spi, bool enable, bool force)
                         * into account.
                         */
                        if (has_acpi_companion(&spi->dev))
-                               gpiod_set_value_cansleep(spi->cs_gpiod, !enable);
+                               gpiod_set_value_cansleep(spi_get_csgpiod(spi, 0), !enable);
                        else
                                /* Polarity handled by GPIO library */
-                               gpiod_set_value_cansleep(spi->cs_gpiod, activate);
+                               gpiod_set_value_cansleep(spi_get_csgpiod(spi, 0), activate);
                }
                /* Some SPI masters need both GPIO CS & slave_select */
                if ((spi->controller->flags & SPI_MASTER_GPIO_SS) &&
@@ -1013,7 +1012,7 @@ static void spi_set_cs(struct spi_device *spi, bool enable, bool force)
                spi->controller->set_cs(spi, !enable);
        }
 
-       if (spi->cs_gpiod || !spi->controller->set_cs_timing) {
+       if (spi_get_csgpiod(spi, 0) || !spi->controller->set_cs_timing) {
                if (activate)
                        spi_delay_exec(&spi->cs_setup, NULL);
                else
@@ -1484,6 +1483,13 @@ static void _spi_transfer_cs_change_delay(struct spi_message *msg,
        }
 }
 
+void spi_transfer_cs_change_delay_exec(struct spi_message *msg,
+                                                 struct spi_transfer *xfer)
+{
+       _spi_transfer_cs_change_delay(msg, xfer);
+}
+EXPORT_SYMBOL_GPL(spi_transfer_cs_change_delay_exec);
+
 /*
  * spi_transfer_one_message - Default implementation of transfer_one_message()
  *
@@ -1921,7 +1927,7 @@ void spi_take_timestamp_post(struct spi_controller *ctlr,
        /* Capture the resolution of the timestamp */
        xfer->ptp_sts_word_post = progress;
 
-       xfer->timestamped = true;
+       xfer->timestamped = 1;
 }
 EXPORT_SYMBOL_GPL(spi_take_timestamp_post);
 
@@ -2220,11 +2226,26 @@ void spi_flush_queue(struct spi_controller *ctlr)
 /*-------------------------------------------------------------------------*/
 
 #if defined(CONFIG_OF)
+static void of_spi_parse_dt_cs_delay(struct device_node *nc,
+                                    struct spi_delay *delay, const char *prop)
+{
+       u32 value;
+
+       if (!of_property_read_u32(nc, prop, &value)) {
+               if (value > U16_MAX) {
+                       delay->value = DIV_ROUND_UP(value, 1000);
+                       delay->unit = SPI_DELAY_UNIT_USECS;
+               } else {
+                       delay->value = value;
+                       delay->unit = SPI_DELAY_UNIT_NSECS;
+               }
+       }
+}
+
 static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi,
                           struct device_node *nc)
 {
        u32 value;
-       u16 cs_setup;
        int rc;
 
        /* Mode (clock phase/polarity/etc.) */
@@ -2304,16 +2325,16 @@ static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi,
                        nc, rc);
                return rc;
        }
-       spi->chip_select = value;
+       spi_set_chipselect(spi, 0, value);
 
        /* Device speed */
        if (!of_property_read_u32(nc, "spi-max-frequency", &value))
                spi->max_speed_hz = value;
 
-       if (!of_property_read_u16(nc, "spi-cs-setup-ns", &cs_setup)) {
-               spi->cs_setup.value = cs_setup;
-               spi->cs_setup.unit = SPI_DELAY_UNIT_NSECS;
-       }
+       /* Device CS delays */
+       of_spi_parse_dt_cs_delay(nc, &spi->cs_setup, "spi-cs-setup-delay-ns");
+       of_spi_parse_dt_cs_delay(nc, &spi->cs_hold, "spi-cs-hold-delay-ns");
+       of_spi_parse_dt_cs_delay(nc, &spi->cs_inactive, "spi-cs-inactive-delay-ns");
 
        return 0;
 }
@@ -2423,7 +2444,7 @@ struct spi_device *spi_new_ancillary_device(struct spi_device *spi,
        strscpy(ancillary->modalias, "dummy", sizeof(ancillary->modalias));
 
        /* Use provided chip-select for ancillary device */
-       ancillary->chip_select = chip_select;
+       spi_set_chipselect(ancillary, 0, chip_select);
 
        /* Take over SPI mode/speed from SPI main device */
        ancillary->max_speed_hz = spi->max_speed_hz;
@@ -2670,7 +2691,7 @@ struct spi_device *acpi_spi_device_alloc(struct spi_controller *ctlr,
        spi->mode               |= lookup.mode;
        spi->irq                = lookup.irq;
        spi->bits_per_word      = lookup.bits_per_word;
-       spi->chip_select        = lookup.chip_select;
+       spi_set_chipselect(spi, 0, lookup.chip_select);
 
        return spi;
 }
@@ -3051,15 +3072,14 @@ static int spi_controller_check_ops(struct spi_controller *ctlr)
         * The controller may implement only the high-level SPI-memory like
         * operations if it does not support regular SPI transfers, and this is
         * valid use case.
-        * If ->mem_ops is NULL, we request that at least one of the
-        * ->transfer_xxx() method be implemented.
+        * If ->mem_ops or ->mem_ops->exec_op is NULL, we request that at least
+        * one of the ->transfer_xxx() method be implemented.
         */
-       if (ctlr->mem_ops) {
-               if (!ctlr->mem_ops->exec_op)
-                       return -EINVAL;
-       } else if (!ctlr->transfer && !ctlr->transfer_one &&
+       if (!ctlr->mem_ops || (ctlr->mem_ops && !ctlr->mem_ops->exec_op)) {
+               if (!ctlr->transfer && !ctlr->transfer_one &&
                   !ctlr->transfer_one_message) {
-               return -EINVAL;
+                       return -EINVAL;
+               }
        }
 
        return 0;
@@ -3632,7 +3652,7 @@ static int spi_set_cs_timing(struct spi_device *spi)
        struct device *parent = spi->controller->dev.parent;
        int status = 0;
 
-       if (spi->controller->set_cs_timing && !spi->cs_gpiod) {
+       if (spi->controller->set_cs_timing && !spi_get_csgpiod(spi, 0)) {
                if (spi->controller->auto_runtime_pm) {
                        status = pm_runtime_get_sync(parent);
                        if (status < 0) {
@@ -3837,7 +3857,7 @@ static int __spi_validate(struct spi_device *spi, struct spi_message *message)
         * cs_change is set for each transfer.
         */
        if ((spi->mode & SPI_CS_WORD) && (!(ctlr->mode_bits & SPI_CS_WORD) ||
-                                         spi->cs_gpiod)) {
+                                         spi_get_csgpiod(spi, 0))) {
                size_t maxsize;
                int ret;