#include <linux/gpio/consumer.h>
 #include <linux/pinctrl/consumer.h>
 #include <linux/pm_runtime.h>
+#include <linux/iopoll.h>
 #include <trace/events/spi.h>
 
 /* SPI register offsets */
        bool                    keep_cs;
 
        u32                     fifo_size;
+       bool                    last_polarity;
        u8                      native_cs_free;
        u8                      native_cs_for_gpio;
 };
 #define SPI_MAX_DMA_XFER       65535 /* true for both PDC and DMA */
 #define INVALID_DMA_ADDRESS    0xffffffff
 
+/*
+ * This frequency can be anything supported by the controller, but to avoid
+ * unnecessary delay, the highest possible frequency is chosen.
+ *
+ * This frequency is the highest possible which is not interfering with other
+ * chip select registers (see Note for Serial Clock Bit Rate configuration in
+ * Atmel-11121F-ATARM-SAMA5D3-Series-Datasheet_02-Feb-16, page 1283)
+ */
+#define DUMMY_MSG_FREQUENCY    0x02
+/*
+ * 8 bits is the minimum data the controller is capable of sending.
+ *
+ * This message can be anything as it should not be treated by any SPI device.
+ */
+#define DUMMY_MSG              0xAA
+
 /*
  * Version 2 of the SPI controller has
  *  - CR.LASTXFER
        return as->caps.is_spi2;
 }
 
+/*
+ * Send a dummy message.
+ *
+ * This is sometimes needed when using a CS GPIO to force clock transition when
+ * switching between devices with different polarities.
+ */
+static void atmel_spi_send_dummy(struct atmel_spi *as, struct spi_device *spi, int chip_select)
+{
+       u32 status;
+       u32 csr;
+
+       /*
+        * Set a clock frequency to allow sending message on SPI bus.
+        * The frequency here can be anything, but is needed for
+        * the controller to send the data.
+        */
+       csr = spi_readl(as, CSR0 + 4 * chip_select);
+       csr = SPI_BFINS(SCBR, DUMMY_MSG_FREQUENCY, csr);
+       spi_writel(as, CSR0 + 4 * chip_select, csr);
+
+       /*
+        * Read all data coming from SPI bus, needed to be able to send
+        * the message.
+        */
+       spi_readl(as, RDR);
+       while (spi_readl(as, SR) & SPI_BIT(RDRF)) {
+               spi_readl(as, RDR);
+               cpu_relax();
+       }
+
+       spi_writel(as, TDR, DUMMY_MSG);
+
+       readl_poll_timeout_atomic(as->regs + SPI_SR, status,
+                                 (status & SPI_BIT(TXEMPTY)), 1, 1000);
+}
+
+
 /*
  * Earlier SPI controllers (e.g. on at91rm9200) have a design bug whereby
  * they assume that spi slave device state will not change on deselect, so
  * Master on Chip Select 0.")  No workaround exists for that ... so for
  * nCS0 on that chip, we (a) don't use the GPIO, (b) can't support CS_HIGH,
  * and (c) will trigger that first erratum in some cases.
+ *
+ * When changing the clock polarity, the SPI controller waits for the next
+ * transmission to enforce the default clock state. This may be an issue when
+ * using a GPIO as Chip Select: the clock level is applied only when the first
+ * packet is sent, once the CS has already been asserted. The workaround is to
+ * avoid this by sending a first (dummy) message before toggling the CS state.
  */
-
 static void cs_activate(struct atmel_spi *as, struct spi_device *spi)
 {
        struct atmel_spi_device *asd = spi->controller_state;
+       bool new_polarity;
        int chip_select;
        u32 mr;
 
                }
 
                mr = spi_readl(as, MR);
+
+               /*
+                * Ensures the clock polarity is valid before we actually
+                * assert the CS to avoid spurious clock edges to be
+                * processed by the spi devices.
+                */
+               if (spi_get_csgpiod(spi, 0)) {
+                       new_polarity = (asd->csr & SPI_BIT(CPOL)) != 0;
+                       if (new_polarity != as->last_polarity) {
+                               /*
+                                * Need to disable the GPIO before sending the dummy
+                                * message because it is already set by the spi core.
+                                */
+                               gpiod_set_value_cansleep(spi_get_csgpiod(spi, 0), 0);
+                               atmel_spi_send_dummy(as, spi, chip_select);
+                               as->last_polarity = new_polarity;
+                               gpiod_set_value_cansleep(spi_get_csgpiod(spi, 0), 1);
+                       }
+               }
        } else {
                u32 cpol = (spi->mode & SPI_CPOL) ? SPI_BIT(CPOL) : 0;
                int i;