Commit c7a40259 authored by Clark Wang's avatar Clark Wang Committed by Mark Brown

spi: lpspi: use the core way to implement cs-gpio function

Use the default implementation of transfer_one_msg/chipselect/setup
functions in spi core to implement cs-gpio control.
Use fsl_lpspi_prepare_message to init the cs_gpio pin.
Signed-off-by: default avatarClark Wang <xiaoning.wang@nxp.com>
Acked-by: default avatarFugang Duan <Fugang.duan@nxp.com>
Signed-off-by: default avatarMark Brown <broonie@kernel.org>
parent 77736a98
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include <linux/completion.h> #include <linux/completion.h>
#include <linux/delay.h> #include <linux/delay.h>
#include <linux/err.h> #include <linux/err.h>
#include <linux/gpio.h>
#include <linux/interrupt.h> #include <linux/interrupt.h>
#include <linux/io.h> #include <linux/io.h>
#include <linux/irq.h> #include <linux/irq.h>
...@@ -16,8 +17,10 @@ ...@@ -16,8 +17,10 @@
#include <linux/module.h> #include <linux/module.h>
#include <linux/of.h> #include <linux/of.h>
#include <linux/of_device.h> #include <linux/of_device.h>
#include <linux/of_gpio.h>
#include <linux/pinctrl/consumer.h> #include <linux/pinctrl/consumer.h>
#include <linux/platform_device.h> #include <linux/platform_device.h>
#include <linux/platform_data/spi-imx.h>
#include <linux/pm_runtime.h> #include <linux/pm_runtime.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/spi/spi.h> #include <linux/spi/spi.h>
...@@ -91,6 +94,7 @@ struct fsl_lpspi_data { ...@@ -91,6 +94,7 @@ struct fsl_lpspi_data {
struct clk *clk_ipg; struct clk *clk_ipg;
struct clk *clk_per; struct clk *clk_per;
bool is_slave; bool is_slave;
bool is_first_byte;
void *rx_buf; void *rx_buf;
const void *tx_buf; const void *tx_buf;
...@@ -106,6 +110,8 @@ struct fsl_lpspi_data { ...@@ -106,6 +110,8 @@ struct fsl_lpspi_data {
struct completion xfer_done; struct completion xfer_done;
bool slave_aborted; bool slave_aborted;
int chipselect[0];
}; };
static const struct of_device_id fsl_lpspi_dt_ids[] = { static const struct of_device_id fsl_lpspi_dt_ids[] = {
...@@ -178,6 +184,20 @@ static int lpspi_unprepare_xfer_hardware(struct spi_controller *controller) ...@@ -178,6 +184,20 @@ static int lpspi_unprepare_xfer_hardware(struct spi_controller *controller)
return 0; return 0;
} }
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, spi->mode & SPI_CS_HIGH ? 0 : 1);
return 0;
}
static void fsl_lpspi_write_tx_fifo(struct fsl_lpspi_data *fsl_lpspi) static void fsl_lpspi_write_tx_fifo(struct fsl_lpspi_data *fsl_lpspi)
{ {
u8 txfifo_cnt; u8 txfifo_cnt;
...@@ -210,8 +230,7 @@ static void fsl_lpspi_read_rx_fifo(struct fsl_lpspi_data *fsl_lpspi) ...@@ -210,8 +230,7 @@ static void fsl_lpspi_read_rx_fifo(struct fsl_lpspi_data *fsl_lpspi)
fsl_lpspi->rx(fsl_lpspi); fsl_lpspi->rx(fsl_lpspi);
} }
static void fsl_lpspi_set_cmd(struct fsl_lpspi_data *fsl_lpspi, static void fsl_lpspi_set_cmd(struct fsl_lpspi_data *fsl_lpspi)
bool is_first_xfer)
{ {
u32 temp = 0; u32 temp = 0;
...@@ -227,7 +246,7 @@ static void fsl_lpspi_set_cmd(struct fsl_lpspi_data *fsl_lpspi, ...@@ -227,7 +246,7 @@ static void fsl_lpspi_set_cmd(struct fsl_lpspi_data *fsl_lpspi,
* For subsequent transfer, set TCR_CONTC to keep SS asserted. * For subsequent transfer, set TCR_CONTC to keep SS asserted.
*/ */
temp |= TCR_CONT; temp |= TCR_CONT;
if (is_first_xfer) if (fsl_lpspi->is_first_byte)
temp &= ~TCR_CONTC; temp &= ~TCR_CONTC;
else else
temp |= TCR_CONTC; temp |= TCR_CONTC;
...@@ -396,8 +415,7 @@ static int fsl_lpspi_reset(struct fsl_lpspi_data *fsl_lpspi) ...@@ -396,8 +415,7 @@ static int fsl_lpspi_reset(struct fsl_lpspi_data *fsl_lpspi)
return 0; return 0;
} }
static int fsl_lpspi_transfer_one(struct spi_controller *controller, static int fsl_lpspi_pio_transfer(struct spi_controller *controller,
struct spi_device *spi,
struct spi_transfer *t) struct spi_transfer *t)
{ {
struct fsl_lpspi_data *fsl_lpspi = struct fsl_lpspi_data *fsl_lpspi =
...@@ -422,40 +440,27 @@ static int fsl_lpspi_transfer_one(struct spi_controller *controller, ...@@ -422,40 +440,27 @@ static int fsl_lpspi_transfer_one(struct spi_controller *controller,
return 0; return 0;
} }
static int fsl_lpspi_transfer_one_msg(struct spi_controller *controller, static int fsl_lpspi_transfer_one(struct spi_controller *controller,
struct spi_message *msg) struct spi_device *spi,
struct spi_transfer *t)
{ {
struct fsl_lpspi_data *fsl_lpspi = struct fsl_lpspi_data *fsl_lpspi =
spi_controller_get_devdata(controller); spi_controller_get_devdata(controller);
struct spi_device *spi = msg->spi; int ret;
struct spi_transfer *xfer;
bool is_first_xfer = true;
int ret = 0;
msg->status = 0;
msg->actual_length = 0;
list_for_each_entry(xfer, &msg->transfers, transfer_list) {
ret = fsl_lpspi_setup_transfer(spi, xfer);
if (ret < 0)
goto complete;
fsl_lpspi_set_cmd(fsl_lpspi, is_first_xfer);
is_first_xfer = false;
ret = fsl_lpspi_transfer_one(controller, spi, xfer); fsl_lpspi->is_first_byte = true;
if (ret < 0) ret = fsl_lpspi_setup_transfer(spi, t);
goto complete; if (ret < 0)
return ret;
msg->actual_length += xfer->len; fsl_lpspi_set_cmd(fsl_lpspi);
} fsl_lpspi->is_first_byte = false;
complete: ret = fsl_lpspi_pio_transfer(controller, t);
msg->status = ret; if (ret < 0)
spi_finalize_current_message(controller); return ret;
return ret; return 0;
} }
static irqreturn_t fsl_lpspi_isr(int irq, void *dev_id) static irqreturn_t fsl_lpspi_isr(int irq, void *dev_id)
...@@ -531,10 +536,13 @@ static int fsl_lpspi_init_rpm(struct fsl_lpspi_data *fsl_lpspi) ...@@ -531,10 +536,13 @@ static int fsl_lpspi_init_rpm(struct fsl_lpspi_data *fsl_lpspi)
static int fsl_lpspi_probe(struct platform_device *pdev) static int fsl_lpspi_probe(struct platform_device *pdev)
{ {
struct device_node *np = pdev->dev.of_node;
struct fsl_lpspi_data *fsl_lpspi; struct fsl_lpspi_data *fsl_lpspi;
struct spi_controller *controller; struct spi_controller *controller;
struct spi_imx_master *lpspi_platform_info =
dev_get_platdata(&pdev->dev);
struct resource *res; struct resource *res;
int ret, irq; int i, ret, irq;
u32 temp; u32 temp;
if (of_property_read_bool((&pdev->dev)->of_node, "spi-slave")) if (of_property_read_bool((&pdev->dev)->of_node, "spi-slave"))
...@@ -558,7 +566,30 @@ static int fsl_lpspi_probe(struct platform_device *pdev) ...@@ -558,7 +566,30 @@ static int fsl_lpspi_probe(struct platform_device *pdev)
fsl_lpspi->is_slave = of_property_read_bool((&pdev->dev)->of_node, fsl_lpspi->is_slave = of_property_read_bool((&pdev->dev)->of_node,
"spi-slave"); "spi-slave");
controller->transfer_one_message = fsl_lpspi_transfer_one_msg; 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->cs_gpios = fsl_lpspi->chipselect;
controller->prepare_message = fsl_lpspi_prepare_message;
}
controller->transfer_one = fsl_lpspi_transfer_one;
controller->prepare_transfer_hardware = lpspi_prepare_xfer_hardware; controller->prepare_transfer_hardware = lpspi_prepare_xfer_hardware;
controller->unprepare_transfer_hardware = lpspi_unprepare_xfer_hardware; controller->unprepare_transfer_hardware = lpspi_unprepare_xfer_hardware;
controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH; controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment