summaryrefslogtreecommitdiffstats
path: root/drivers/spi
diff options
context:
space:
mode:
authorGeert Uytterhoeven <geert+renesas@glider.be>2017-12-13 14:05:12 -0500
committerMark Brown <broonie@kernel.org>2017-12-14 06:46:14 -0500
commitb8761434bdec32fa46a644c26a12d16a9b0f58d8 (patch)
treeb8007881f64e76e6c969069844c6d554c16ce389 /drivers/spi
parent9cce882bedd2768dc251b73f2ad86a9bfcfd9fc7 (diff)
spi: sh-msiof: Implement cs-gpios configuration
The current support for GPIO chip selects assumes the GPIOs have been configured by platform code or the boot loader. This includes pinmux setup and GPIO direction. Hence it does not work as expected when just described in DT using the "cs-gpios" property. Fix this by: 1. using devm_gpiod_get_index() to request the GPIO, and thus configure pinmux, if needed, 2. configuring the GPIO direction is the spi_master.setup() callback. Use gpio_is_valid() instead of a check on positive numbers. Note that when using GPIO chip selects, at least one native chip select must be left unused, as that native chip select will be driven anyway, and (global) native chip select polarity must be taken into account. Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be> Signed-off-by: Mark Brown <broonie@kernel.org>
Diffstat (limited to 'drivers/spi')
-rw-r--r--drivers/spi/spi-sh-msiof.c66
1 files changed, 59 insertions, 7 deletions
diff --git a/drivers/spi/spi-sh-msiof.c b/drivers/spi/spi-sh-msiof.c
index 9bdc292aa050..8aa5c7b910d9 100644
--- a/drivers/spi/spi-sh-msiof.c
+++ b/drivers/spi/spi-sh-msiof.c
@@ -19,6 +19,7 @@
19#include <linux/dmaengine.h> 19#include <linux/dmaengine.h>
20#include <linux/err.h> 20#include <linux/err.h>
21#include <linux/gpio.h> 21#include <linux/gpio.h>
22#include <linux/gpio/consumer.h>
22#include <linux/interrupt.h> 23#include <linux/interrupt.h>
23#include <linux/io.h> 24#include <linux/io.h>
24#include <linux/kernel.h> 25#include <linux/kernel.h>
@@ -55,6 +56,7 @@ struct sh_msiof_spi_priv {
55 void *rx_dma_page; 56 void *rx_dma_page;
56 dma_addr_t tx_dma_addr; 57 dma_addr_t tx_dma_addr;
57 dma_addr_t rx_dma_addr; 58 dma_addr_t rx_dma_addr;
59 unsigned short unused_ss;
58 bool native_cs_inited; 60 bool native_cs_inited;
59 bool native_cs_high; 61 bool native_cs_high;
60 bool slave_aborted; 62 bool slave_aborted;
@@ -547,8 +549,8 @@ static int sh_msiof_spi_setup(struct spi_device *spi)
547 spi->cs_gpio = (uintptr_t)spi->controller_data; 549 spi->cs_gpio = (uintptr_t)spi->controller_data;
548 } 550 }
549 551
550 if (spi->cs_gpio >= 0) { 552 if (gpio_is_valid(spi->cs_gpio)) {
551 gpio_set_value(spi->cs_gpio, !(spi->mode & SPI_CS_HIGH)); 553 gpio_direction_output(spi->cs_gpio, !(spi->mode & SPI_CS_HIGH));
552 return 0; 554 return 0;
553 } 555 }
554 556
@@ -580,14 +582,20 @@ static int sh_msiof_prepare_message(struct spi_master *master,
580{ 582{
581 struct sh_msiof_spi_priv *p = spi_master_get_devdata(master); 583 struct sh_msiof_spi_priv *p = spi_master_get_devdata(master);
582 const struct spi_device *spi = msg->spi; 584 const struct spi_device *spi = msg->spi;
585 u32 ss, cs_high;
583 586
584 /* Configure pins before asserting CS */ 587 /* Configure pins before asserting CS */
585 sh_msiof_spi_set_pin_regs(p, spi->chip_select, 588 if (gpio_is_valid(spi->cs_gpio)) {
586 !!(spi->mode & SPI_CPOL), 589 ss = p->unused_ss;
590 cs_high = p->native_cs_high;
591 } else {
592 ss = spi->chip_select;
593 cs_high = !!(spi->mode & SPI_CS_HIGH);
594 }
595 sh_msiof_spi_set_pin_regs(p, ss, !!(spi->mode & SPI_CPOL),
587 !!(spi->mode & SPI_CPHA), 596 !!(spi->mode & SPI_CPHA),
588 !!(spi->mode & SPI_3WIRE), 597 !!(spi->mode & SPI_3WIRE),
589 !!(spi->mode & SPI_LSB_FIRST), 598 !!(spi->mode & SPI_LSB_FIRST), cs_high);
590 !!(spi->mode & SPI_CS_HIGH));
591 return 0; 599 return 0;
592} 600}
593 601
@@ -1091,6 +1099,45 @@ static struct sh_msiof_spi_info *sh_msiof_spi_parse_dt(struct device *dev)
1091} 1099}
1092#endif 1100#endif
1093 1101
1102static int sh_msiof_get_cs_gpios(struct sh_msiof_spi_priv *p)
1103{
1104 struct device *dev = &p->pdev->dev;
1105 unsigned int used_ss_mask = 0;
1106 unsigned int cs_gpios = 0;
1107 unsigned int num_cs, i;
1108 int ret;
1109
1110 ret = gpiod_count(dev, "cs");
1111 if (ret <= 0)
1112 return 0;
1113
1114 num_cs = max_t(unsigned int, ret, p->master->num_chipselect);
1115 for (i = 0; i < num_cs; i++) {
1116 struct gpio_desc *gpiod;
1117
1118 gpiod = devm_gpiod_get_index(dev, "cs", i, GPIOD_ASIS);
1119 if (!IS_ERR(gpiod)) {
1120 cs_gpios++;
1121 continue;
1122 }
1123
1124 if (PTR_ERR(gpiod) != -ENOENT)
1125 return PTR_ERR(gpiod);
1126
1127 if (i >= MAX_SS) {
1128 dev_err(dev, "Invalid native chip select %d\n", i);
1129 return -EINVAL;
1130 }
1131 used_ss_mask |= BIT(i);
1132 }
1133 p->unused_ss = ffz(used_ss_mask);
1134 if (cs_gpios && p->unused_ss >= MAX_SS) {
1135 dev_err(dev, "No unused native chip select available\n");
1136 return -EINVAL;
1137 }
1138 return 0;
1139}
1140
1094static struct dma_chan *sh_msiof_request_dma_chan(struct device *dev, 1141static struct dma_chan *sh_msiof_request_dma_chan(struct device *dev,
1095 enum dma_transfer_direction dir, unsigned int id, dma_addr_t port_addr) 1142 enum dma_transfer_direction dir, unsigned int id, dma_addr_t port_addr)
1096{ 1143{
@@ -1304,13 +1351,18 @@ static int sh_msiof_spi_probe(struct platform_device *pdev)
1304 if (p->info->rx_fifo_override) 1351 if (p->info->rx_fifo_override)
1305 p->rx_fifo_size = p->info->rx_fifo_override; 1352 p->rx_fifo_size = p->info->rx_fifo_override;
1306 1353
1354 /* Setup GPIO chip selects */
1355 master->num_chipselect = p->info->num_chipselect;
1356 ret = sh_msiof_get_cs_gpios(p);
1357 if (ret)
1358 goto err1;
1359
1307 /* init master code */ 1360 /* init master code */
1308 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH; 1361 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
1309 master->mode_bits |= SPI_LSB_FIRST | SPI_3WIRE; 1362 master->mode_bits |= SPI_LSB_FIRST | SPI_3WIRE;
1310 master->flags = chipdata->master_flags; 1363 master->flags = chipdata->master_flags;
1311 master->bus_num = pdev->id; 1364 master->bus_num = pdev->id;
1312 master->dev.of_node = pdev->dev.of_node; 1365 master->dev.of_node = pdev->dev.of_node;
1313 master->num_chipselect = p->info->num_chipselect;
1314 master->setup = sh_msiof_spi_setup; 1366 master->setup = sh_msiof_spi_setup;
1315 master->prepare_message = sh_msiof_prepare_message; 1367 master->prepare_message = sh_msiof_prepare_message;
1316 master->slave_abort = sh_msiof_slave_abort; 1368 master->slave_abort = sh_msiof_slave_abort;