MLK-20441-1 spi: lpspi: Add cs-gpio feature to support spi-nor
authorClark Wang <xiaoning.wang@nxp.com>
Wed, 28 Nov 2018 09:14:42 +0000 (17:14 +0800)
committerLeonard Crestez <leonard.crestez@nxp.com>
Wed, 17 Apr 2019 23:51:34 +0000 (02:51 +0300)
Add cs-gpio feature. Use fsl_lpspi_prepare_message() and
fsl_lpspi_unprepare_message() to enable and control cs line.
These two functions will be only called at the beginning and the ending
of a message transfer.

Still can use the mode without cs-gpio. The two ways can refer to
fsl-imx8qm-lpddr4-arm2-lpspi.dts for setting the lpspi configration in
dts file.

Signed-off-by: Clark Wang <xiaoning.wang@nxp.com>
(cherry picked from commit 245d353cdf0a461ddcd2dcc36793571aae1069b0)

drivers/spi/spi-fsl-lpspi.c

index 0d7f145..96db64e 100644 (file)
@@ -8,6 +8,7 @@
 #include <linux/completion.h>
 #include <linux/delay.h>
 #include <linux/err.h>
+#include <linux/gpio.h>
 #include <linux/interrupt.h>
 #include <linux/io.h>
 #include <linux/irq.h>
 #include <linux/module.h>
 #include <linux/of.h>
 #include <linux/of_device.h>
+#include <linux/of_gpio.h>
 #include <linux/pinctrl/consumer.h>
 #include <linux/platform_device.h>
+#include <linux/platform_data/spi-imx.h>
 #include <linux/slab.h>
 #include <linux/spi/spi.h>
 #include <linux/spi/spi_bitbang.h>
 
 #define FSL_LPSPI_RPM_TIMEOUT 50 /* 50ms */
 
+#define LPSPI_CS_ACTIVE                1
+#define LPSPI_CS_INACTIVE      0
+#define LPSPI_CS_DELAY         100
+
 /* i.MX7ULP LPSPI registers */
 #define IMX7ULP_VERID  0x0
 #define IMX7ULP_PARAM  0x4
@@ -103,6 +110,8 @@ struct fsl_lpspi_data {
        struct completion xfer_done;
 
        bool slave_aborted;
+
+       int chipselect[0];
 };
 
 static const struct of_device_id fsl_lpspi_dt_ids[] = {
@@ -175,6 +184,47 @@ static int lpspi_unprepare_xfer_hardware(struct spi_controller *controller)
        return 0;
 }
 
+static void fsl_lpspi_chipselect(struct spi_device *spi, bool enable)
+{
+       struct fsl_lpspi_data *fsl_lpspi =
+                               spi_controller_get_devdata(spi->controller);
+       int gpio = fsl_lpspi->chipselect[spi->chip_select];
+
+       enable = (!!(spi->mode & SPI_CS_HIGH) == enable);
+
+       if (!gpio_is_valid(gpio))
+               return;
+
+       gpio_set_value_cansleep(gpio, enable);
+}
+
+static int
+fsl_lpspi_prepare_message(struct spi_controller *controller, struct spi_message *msg)
+{
+       struct fsl_lpspi_data *fsl_lpspi = spi_controller_get_devdata(controller);
+       struct spi_device *spi = msg->spi;
+       int gpio = fsl_lpspi->chipselect[spi->chip_select];
+
+       if (gpio_is_valid(gpio)) {
+               gpio_direction_output(gpio,
+                                     fsl_lpspi->config.mode & SPI_CS_HIGH ? 0 : 1);
+       }
+
+       fsl_lpspi_chipselect(spi, LPSPI_CS_ACTIVE);
+
+       return 0;
+}
+
+static int
+fsl_lpspi_unprepare_message(struct spi_controller *controller, struct spi_message *msg)
+{
+       struct spi_device *spi = msg->spi;
+
+       fsl_lpspi_chipselect(spi, LPSPI_CS_INACTIVE);
+
+       return 0;
+}
+
 static void fsl_lpspi_write_tx_fifo(struct fsl_lpspi_data *fsl_lpspi)
 {
        u8 txfifo_cnt;
@@ -376,6 +426,9 @@ static int fsl_lpspi_reset(struct fsl_lpspi_data *fsl_lpspi)
 {
        u32 temp;
 
+       /* Disable all interrupt */
+       fsl_lpspi_intctrl(fsl_lpspi, 0);
+
        /* W1C for all flags in SR */
        temp = 0x3F << 8;
        writel(temp, fsl_lpspi->base + IMX7ULP_SR);
@@ -512,10 +565,13 @@ static int fsl_lpspi_init_rpm(struct fsl_lpspi_data *fsl_lpspi)
 
 static int fsl_lpspi_probe(struct platform_device *pdev)
 {
+       struct device_node *np = pdev->dev.of_node;
        struct fsl_lpspi_data *fsl_lpspi;
        struct spi_controller *controller;
+       struct spi_imx_master *lpspi_platform_info =
+               dev_get_platdata(&pdev->dev);
        struct resource *res;
-       int ret, irq;
+       int i, ret, irq;
        u32 temp;
 
        if (of_property_read_bool((&pdev->dev)->of_node, "spi-slave"))
@@ -539,6 +595,29 @@ static int fsl_lpspi_probe(struct platform_device *pdev)
        fsl_lpspi->is_slave = of_property_read_bool((&pdev->dev)->of_node,
                                                    "spi-slave");
 
+       if (!fsl_lpspi->is_slave) {
+               for (i = 0; i < controller->num_chipselect; i++) {
+                       int cs_gpio = of_get_named_gpio(np, "cs-gpios", i);
+
+                       if (!gpio_is_valid(cs_gpio) && lpspi_platform_info)
+                               cs_gpio = lpspi_platform_info->chipselect[i];
+
+                       fsl_lpspi->chipselect[i] = cs_gpio;
+                       if (!gpio_is_valid(cs_gpio))
+                               continue;
+
+                       ret = devm_gpio_request(&pdev->dev, fsl_lpspi->chipselect[i],
+                                               DRIVER_NAME);
+                       if (ret) {
+                               dev_err(&pdev->dev, "can't get cs gpios\n");
+                               goto out_controller_put;
+                       }
+               }
+
+               controller->prepare_message = fsl_lpspi_prepare_message;
+               controller->unprepare_message = fsl_lpspi_unprepare_message;
+       }
+
        controller->transfer_one_message = fsl_lpspi_transfer_one_msg;
        controller->prepare_transfer_hardware = lpspi_prepare_xfer_hardware;
        controller->unprepare_transfer_hardware = lpspi_unprepare_xfer_hardware;