diff options
Diffstat (limited to 'drivers/spi/spi-davinci.c')
-rw-r--r-- | drivers/spi/spi-davinci.c | 72 |
1 files changed, 53 insertions, 19 deletions
diff --git a/drivers/spi/spi-davinci.c b/drivers/spi/spi-davinci.c index 50f750989258..276a3884fb3c 100644 --- a/drivers/spi/spi-davinci.c +++ b/drivers/spi/spi-davinci.c | |||
@@ -30,6 +30,7 @@ | |||
30 | #include <linux/edma.h> | 30 | #include <linux/edma.h> |
31 | #include <linux/of.h> | 31 | #include <linux/of.h> |
32 | #include <linux/of_device.h> | 32 | #include <linux/of_device.h> |
33 | #include <linux/of_gpio.h> | ||
33 | #include <linux/spi/spi.h> | 34 | #include <linux/spi/spi.h> |
34 | #include <linux/spi/spi_bitbang.h> | 35 | #include <linux/spi/spi_bitbang.h> |
35 | #include <linux/slab.h> | 36 | #include <linux/slab.h> |
@@ -38,8 +39,6 @@ | |||
38 | 39 | ||
39 | #define SPI_NO_RESOURCE ((resource_size_t)-1) | 40 | #define SPI_NO_RESOURCE ((resource_size_t)-1) |
40 | 41 | ||
41 | #define SPI_MAX_CHIPSELECT 2 | ||
42 | |||
43 | #define CS_DEFAULT 0xFF | 42 | #define CS_DEFAULT 0xFF |
44 | 43 | ||
45 | #define SPIFMT_PHASE_MASK BIT(16) | 44 | #define SPIFMT_PHASE_MASK BIT(16) |
@@ -142,7 +141,7 @@ struct davinci_spi { | |||
142 | void (*get_rx)(u32 rx_data, struct davinci_spi *); | 141 | void (*get_rx)(u32 rx_data, struct davinci_spi *); |
143 | u32 (*get_tx)(struct davinci_spi *); | 142 | u32 (*get_tx)(struct davinci_spi *); |
144 | 143 | ||
145 | u8 bytes_per_word[SPI_MAX_CHIPSELECT]; | 144 | u8 *bytes_per_word; |
146 | }; | 145 | }; |
147 | 146 | ||
148 | static struct davinci_spi_config davinci_spi_default_cfg; | 147 | static struct davinci_spi_config davinci_spi_default_cfg; |
@@ -213,13 +212,16 @@ static void davinci_spi_chipselect(struct spi_device *spi, int value) | |||
213 | u8 chip_sel = spi->chip_select; | 212 | u8 chip_sel = spi->chip_select; |
214 | u16 spidat1 = CS_DEFAULT; | 213 | u16 spidat1 = CS_DEFAULT; |
215 | bool gpio_chipsel = false; | 214 | bool gpio_chipsel = false; |
215 | int gpio; | ||
216 | 216 | ||
217 | dspi = spi_master_get_devdata(spi->master); | 217 | dspi = spi_master_get_devdata(spi->master); |
218 | pdata = &dspi->pdata; | 218 | pdata = &dspi->pdata; |
219 | 219 | ||
220 | if (pdata->chip_sel && chip_sel < pdata->num_chipselect && | 220 | if (spi->cs_gpio >= 0) { |
221 | pdata->chip_sel[chip_sel] != SPI_INTERN_CS) | 221 | /* SPI core parse and update master->cs_gpio */ |
222 | gpio_chipsel = true; | 222 | gpio_chipsel = true; |
223 | gpio = spi->cs_gpio; | ||
224 | } | ||
223 | 225 | ||
224 | /* | 226 | /* |
225 | * Board specific chip select logic decides the polarity and cs | 227 | * Board specific chip select logic decides the polarity and cs |
@@ -227,9 +229,9 @@ static void davinci_spi_chipselect(struct spi_device *spi, int value) | |||
227 | */ | 229 | */ |
228 | if (gpio_chipsel) { | 230 | if (gpio_chipsel) { |
229 | if (value == BITBANG_CS_ACTIVE) | 231 | if (value == BITBANG_CS_ACTIVE) |
230 | gpio_set_value(pdata->chip_sel[chip_sel], 0); | 232 | gpio_set_value(gpio, spi->mode & SPI_CS_HIGH); |
231 | else | 233 | else |
232 | gpio_set_value(pdata->chip_sel[chip_sel], 1); | 234 | gpio_set_value(gpio, !(spi->mode & SPI_CS_HIGH)); |
233 | } else { | 235 | } else { |
234 | if (value == BITBANG_CS_ACTIVE) { | 236 | if (value == BITBANG_CS_ACTIVE) { |
235 | spidat1 |= SPIDAT1_CSHOLD_MASK; | 237 | spidat1 |= SPIDAT1_CSHOLD_MASK; |
@@ -392,17 +394,40 @@ static int davinci_spi_setup(struct spi_device *spi) | |||
392 | int retval = 0; | 394 | int retval = 0; |
393 | struct davinci_spi *dspi; | 395 | struct davinci_spi *dspi; |
394 | struct davinci_spi_platform_data *pdata; | 396 | struct davinci_spi_platform_data *pdata; |
397 | struct spi_master *master = spi->master; | ||
398 | struct device_node *np = spi->dev.of_node; | ||
399 | bool internal_cs = true; | ||
400 | unsigned long flags = GPIOF_DIR_OUT; | ||
395 | 401 | ||
396 | dspi = spi_master_get_devdata(spi->master); | 402 | dspi = spi_master_get_devdata(spi->master); |
397 | pdata = &dspi->pdata; | 403 | pdata = &dspi->pdata; |
398 | 404 | ||
405 | flags |= (spi->mode & SPI_CS_HIGH) ? GPIOF_INIT_LOW : GPIOF_INIT_HIGH; | ||
406 | |||
399 | if (!(spi->mode & SPI_NO_CS)) { | 407 | if (!(spi->mode & SPI_NO_CS)) { |
400 | if ((pdata->chip_sel == NULL) || | 408 | if (np && (master->cs_gpios != NULL) && (spi->cs_gpio >= 0)) { |
401 | (pdata->chip_sel[spi->chip_select] == SPI_INTERN_CS)) | 409 | retval = gpio_request_one(spi->cs_gpio, |
402 | set_io_bits(dspi->base + SPIPC0, 1 << spi->chip_select); | 410 | flags, dev_name(&spi->dev)); |
411 | internal_cs = false; | ||
412 | } else if (pdata->chip_sel && | ||
413 | spi->chip_select < pdata->num_chipselect && | ||
414 | pdata->chip_sel[spi->chip_select] != SPI_INTERN_CS) { | ||
415 | spi->cs_gpio = pdata->chip_sel[spi->chip_select]; | ||
416 | retval = gpio_request_one(spi->cs_gpio, | ||
417 | flags, dev_name(&spi->dev)); | ||
418 | internal_cs = false; | ||
419 | } | ||
420 | } | ||
403 | 421 | ||
422 | if (retval) { | ||
423 | dev_err(&spi->dev, "GPIO %d setup failed (%d)\n", | ||
424 | spi->cs_gpio, retval); | ||
425 | return retval; | ||
404 | } | 426 | } |
405 | 427 | ||
428 | if (internal_cs) | ||
429 | set_io_bits(dspi->base + SPIPC0, 1 << spi->chip_select); | ||
430 | |||
406 | if (spi->mode & SPI_READY) | 431 | if (spi->mode & SPI_READY) |
407 | set_io_bits(dspi->base + SPIPC0, SPIPC0_SPIENA_MASK); | 432 | set_io_bits(dspi->base + SPIPC0, SPIPC0_SPIENA_MASK); |
408 | 433 | ||
@@ -414,6 +439,12 @@ static int davinci_spi_setup(struct spi_device *spi) | |||
414 | return retval; | 439 | return retval; |
415 | } | 440 | } |
416 | 441 | ||
442 | static void davinci_spi_cleanup(struct spi_device *spi) | ||
443 | { | ||
444 | if (spi->cs_gpio >= 0) | ||
445 | gpio_free(spi->cs_gpio); | ||
446 | } | ||
447 | |||
417 | static int davinci_spi_check_error(struct davinci_spi *dspi, int int_status) | 448 | static int davinci_spi_check_error(struct davinci_spi *dspi, int int_status) |
418 | { | 449 | { |
419 | struct device *sdev = dspi->bitbang.master->dev.parent; | 450 | struct device *sdev = dspi->bitbang.master->dev.parent; |
@@ -812,6 +843,8 @@ static int spi_davinci_get_pdata(struct platform_device *pdev, | |||
812 | 843 | ||
813 | /* | 844 | /* |
814 | * default num_cs is 1 and all chipsel are internal to the chip | 845 | * default num_cs is 1 and all chipsel are internal to the chip |
846 | * indicated by chip_sel being NULL or cs_gpios being NULL or | ||
847 | * set to -ENOENT. num-cs includes internal as well as gpios. | ||
815 | * indicated by chip_sel being NULL. GPIO based CS is not | 848 | * indicated by chip_sel being NULL. GPIO based CS is not |
816 | * supported yet in DT bindings. | 849 | * supported yet in DT bindings. |
817 | */ | 850 | */ |
@@ -850,7 +883,7 @@ static int davinci_spi_probe(struct platform_device *pdev) | |||
850 | struct resource *r; | 883 | struct resource *r; |
851 | resource_size_t dma_rx_chan = SPI_NO_RESOURCE; | 884 | resource_size_t dma_rx_chan = SPI_NO_RESOURCE; |
852 | resource_size_t dma_tx_chan = SPI_NO_RESOURCE; | 885 | resource_size_t dma_tx_chan = SPI_NO_RESOURCE; |
853 | int i = 0, ret = 0; | 886 | int ret = 0; |
854 | u32 spipc0; | 887 | u32 spipc0; |
855 | 888 | ||
856 | master = spi_alloc_master(&pdev->dev, sizeof(struct davinci_spi)); | 889 | master = spi_alloc_master(&pdev->dev, sizeof(struct davinci_spi)); |
@@ -876,6 +909,14 @@ static int davinci_spi_probe(struct platform_device *pdev) | |||
876 | /* pdata in dspi is now updated and point pdata to that */ | 909 | /* pdata in dspi is now updated and point pdata to that */ |
877 | pdata = &dspi->pdata; | 910 | pdata = &dspi->pdata; |
878 | 911 | ||
912 | dspi->bytes_per_word = devm_kzalloc(&pdev->dev, | ||
913 | sizeof(*dspi->bytes_per_word) * | ||
914 | pdata->num_chipselect, GFP_KERNEL); | ||
915 | if (dspi->bytes_per_word == NULL) { | ||
916 | ret = -ENOMEM; | ||
917 | goto free_master; | ||
918 | } | ||
919 | |||
879 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); | 920 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
880 | if (r == NULL) { | 921 | if (r == NULL) { |
881 | ret = -ENOENT; | 922 | ret = -ENOENT; |
@@ -915,6 +956,7 @@ static int davinci_spi_probe(struct platform_device *pdev) | |||
915 | master->num_chipselect = pdata->num_chipselect; | 956 | master->num_chipselect = pdata->num_chipselect; |
916 | master->bits_per_word_mask = SPI_BPW_RANGE_MASK(2, 16); | 957 | master->bits_per_word_mask = SPI_BPW_RANGE_MASK(2, 16); |
917 | master->setup = davinci_spi_setup; | 958 | master->setup = davinci_spi_setup; |
959 | master->cleanup = davinci_spi_cleanup; | ||
918 | 960 | ||
919 | dspi->bitbang.chipselect = davinci_spi_chipselect; | 961 | dspi->bitbang.chipselect = davinci_spi_chipselect; |
920 | dspi->bitbang.setup_transfer = davinci_spi_setup_transfer; | 962 | dspi->bitbang.setup_transfer = davinci_spi_setup_transfer; |
@@ -962,14 +1004,6 @@ static int davinci_spi_probe(struct platform_device *pdev) | |||
962 | spipc0 = SPIPC0_DIFUN_MASK | SPIPC0_DOFUN_MASK | SPIPC0_CLKFUN_MASK; | 1004 | spipc0 = SPIPC0_DIFUN_MASK | SPIPC0_DOFUN_MASK | SPIPC0_CLKFUN_MASK; |
963 | iowrite32(spipc0, dspi->base + SPIPC0); | 1005 | iowrite32(spipc0, dspi->base + SPIPC0); |
964 | 1006 | ||
965 | /* initialize chip selects */ | ||
966 | if (pdata->chip_sel) { | ||
967 | for (i = 0; i < pdata->num_chipselect; i++) { | ||
968 | if (pdata->chip_sel[i] != SPI_INTERN_CS) | ||
969 | gpio_direction_output(pdata->chip_sel[i], 1); | ||
970 | } | ||
971 | } | ||
972 | |||
973 | if (pdata->intr_line) | 1007 | if (pdata->intr_line) |
974 | iowrite32(SPI_INTLVL_1, dspi->base + SPILVL); | 1008 | iowrite32(SPI_INTLVL_1, dspi->base + SPILVL); |
975 | else | 1009 | else |