diff options
Diffstat (limited to 'drivers/spi/spi-atmel.c')
-rw-r--r-- | drivers/spi/spi-atmel.c | 93 |
1 files changed, 27 insertions, 66 deletions
diff --git a/drivers/spi/spi-atmel.c b/drivers/spi/spi-atmel.c index 74fddcd3282b..f53f0c5e63da 100644 --- a/drivers/spi/spi-atmel.c +++ b/drivers/spi/spi-atmel.c | |||
@@ -23,8 +23,7 @@ | |||
23 | #include <linux/of.h> | 23 | #include <linux/of.h> |
24 | 24 | ||
25 | #include <linux/io.h> | 25 | #include <linux/io.h> |
26 | #include <linux/gpio.h> | 26 | #include <linux/gpio/consumer.h> |
27 | #include <linux/of_gpio.h> | ||
28 | #include <linux/pinctrl/consumer.h> | 27 | #include <linux/pinctrl/consumer.h> |
29 | #include <linux/pm_runtime.h> | 28 | #include <linux/pm_runtime.h> |
30 | 29 | ||
@@ -312,7 +311,7 @@ struct atmel_spi { | |||
312 | 311 | ||
313 | /* Controller-specific per-slave state */ | 312 | /* Controller-specific per-slave state */ |
314 | struct atmel_spi_device { | 313 | struct atmel_spi_device { |
315 | unsigned int npcs_pin; | 314 | struct gpio_desc *npcs_pin; |
316 | u32 csr; | 315 | u32 csr; |
317 | }; | 316 | }; |
318 | 317 | ||
@@ -355,7 +354,6 @@ static bool atmel_spi_is_v2(struct atmel_spi *as) | |||
355 | static void cs_activate(struct atmel_spi *as, struct spi_device *spi) | 354 | static void cs_activate(struct atmel_spi *as, struct spi_device *spi) |
356 | { | 355 | { |
357 | struct atmel_spi_device *asd = spi->controller_state; | 356 | struct atmel_spi_device *asd = spi->controller_state; |
358 | unsigned active = spi->mode & SPI_CS_HIGH; | ||
359 | u32 mr; | 357 | u32 mr; |
360 | 358 | ||
361 | if (atmel_spi_is_v2(as)) { | 359 | if (atmel_spi_is_v2(as)) { |
@@ -379,7 +377,7 @@ static void cs_activate(struct atmel_spi *as, struct spi_device *spi) | |||
379 | 377 | ||
380 | mr = spi_readl(as, MR); | 378 | mr = spi_readl(as, MR); |
381 | if (as->use_cs_gpios) | 379 | if (as->use_cs_gpios) |
382 | gpio_set_value(asd->npcs_pin, active); | 380 | gpiod_set_value(asd->npcs_pin, 1); |
383 | } else { | 381 | } else { |
384 | u32 cpol = (spi->mode & SPI_CPOL) ? SPI_BIT(CPOL) : 0; | 382 | u32 cpol = (spi->mode & SPI_CPOL) ? SPI_BIT(CPOL) : 0; |
385 | int i; | 383 | int i; |
@@ -396,19 +394,16 @@ static void cs_activate(struct atmel_spi *as, struct spi_device *spi) | |||
396 | mr = spi_readl(as, MR); | 394 | mr = spi_readl(as, MR); |
397 | mr = SPI_BFINS(PCS, ~(1 << spi->chip_select), mr); | 395 | mr = SPI_BFINS(PCS, ~(1 << spi->chip_select), mr); |
398 | if (as->use_cs_gpios && spi->chip_select != 0) | 396 | if (as->use_cs_gpios && spi->chip_select != 0) |
399 | gpio_set_value(asd->npcs_pin, active); | 397 | gpiod_set_value(asd->npcs_pin, 1); |
400 | spi_writel(as, MR, mr); | 398 | spi_writel(as, MR, mr); |
401 | } | 399 | } |
402 | 400 | ||
403 | dev_dbg(&spi->dev, "activate %u%s, mr %08x\n", | 401 | dev_dbg(&spi->dev, "activate NPCS, mr %08x\n", mr); |
404 | asd->npcs_pin, active ? " (high)" : "", | ||
405 | mr); | ||
406 | } | 402 | } |
407 | 403 | ||
408 | static void cs_deactivate(struct atmel_spi *as, struct spi_device *spi) | 404 | static void cs_deactivate(struct atmel_spi *as, struct spi_device *spi) |
409 | { | 405 | { |
410 | struct atmel_spi_device *asd = spi->controller_state; | 406 | struct atmel_spi_device *asd = spi->controller_state; |
411 | unsigned active = spi->mode & SPI_CS_HIGH; | ||
412 | u32 mr; | 407 | u32 mr; |
413 | 408 | ||
414 | /* only deactivate *this* device; sometimes transfers to | 409 | /* only deactivate *this* device; sometimes transfers to |
@@ -420,14 +415,12 @@ static void cs_deactivate(struct atmel_spi *as, struct spi_device *spi) | |||
420 | spi_writel(as, MR, mr); | 415 | spi_writel(as, MR, mr); |
421 | } | 416 | } |
422 | 417 | ||
423 | dev_dbg(&spi->dev, "DEactivate %u%s, mr %08x\n", | 418 | dev_dbg(&spi->dev, "DEactivate NPCS, mr %08x\n", mr); |
424 | asd->npcs_pin, active ? " (low)" : "", | ||
425 | mr); | ||
426 | 419 | ||
427 | if (!as->use_cs_gpios) | 420 | if (!as->use_cs_gpios) |
428 | spi_writel(as, CR, SPI_BIT(LASTXFER)); | 421 | spi_writel(as, CR, SPI_BIT(LASTXFER)); |
429 | else if (atmel_spi_is_v2(as) || spi->chip_select != 0) | 422 | else if (atmel_spi_is_v2(as) || spi->chip_select != 0) |
430 | gpio_set_value(asd->npcs_pin, !active); | 423 | gpiod_set_value(asd->npcs_pin, 0); |
431 | } | 424 | } |
432 | 425 | ||
433 | static void atmel_spi_lock(struct atmel_spi *as) __acquires(&as->lock) | 426 | static void atmel_spi_lock(struct atmel_spi *as) __acquires(&as->lock) |
@@ -1188,7 +1181,6 @@ static int atmel_spi_setup(struct spi_device *spi) | |||
1188 | struct atmel_spi_device *asd; | 1181 | struct atmel_spi_device *asd; |
1189 | u32 csr; | 1182 | u32 csr; |
1190 | unsigned int bits = spi->bits_per_word; | 1183 | unsigned int bits = spi->bits_per_word; |
1191 | unsigned int npcs_pin; | ||
1192 | 1184 | ||
1193 | as = spi_master_get_devdata(spi->master); | 1185 | as = spi_master_get_devdata(spi->master); |
1194 | 1186 | ||
@@ -1217,25 +1209,27 @@ static int atmel_spi_setup(struct spi_device *spi) | |||
1217 | csr |= SPI_BF(DLYBS, 0); | 1209 | csr |= SPI_BF(DLYBS, 0); |
1218 | csr |= SPI_BF(DLYBCT, 0); | 1210 | csr |= SPI_BF(DLYBCT, 0); |
1219 | 1211 | ||
1220 | /* chipselect must have been muxed as GPIO (e.g. in board setup) */ | ||
1221 | npcs_pin = (unsigned long)spi->controller_data; | ||
1222 | |||
1223 | if (!as->use_cs_gpios) | ||
1224 | npcs_pin = spi->chip_select; | ||
1225 | else if (gpio_is_valid(spi->cs_gpio)) | ||
1226 | npcs_pin = spi->cs_gpio; | ||
1227 | |||
1228 | asd = spi->controller_state; | 1212 | asd = spi->controller_state; |
1229 | if (!asd) { | 1213 | if (!asd) { |
1230 | asd = kzalloc(sizeof(struct atmel_spi_device), GFP_KERNEL); | 1214 | asd = kzalloc(sizeof(struct atmel_spi_device), GFP_KERNEL); |
1231 | if (!asd) | 1215 | if (!asd) |
1232 | return -ENOMEM; | 1216 | return -ENOMEM; |
1233 | 1217 | ||
1234 | if (as->use_cs_gpios) | 1218 | /* |
1235 | gpio_direction_output(npcs_pin, | 1219 | * If use_cs_gpios is true this means that we have "cs-gpios" |
1236 | !(spi->mode & SPI_CS_HIGH)); | 1220 | * defined in the device tree node so we should have |
1221 | * gotten the GPIO lines from the device tree inside the | ||
1222 | * SPI core. Warn if this is not the case but continue since | ||
1223 | * CS GPIOs are after all optional. | ||
1224 | */ | ||
1225 | if (as->use_cs_gpios) { | ||
1226 | if (!spi->cs_gpiod) { | ||
1227 | dev_err(&spi->dev, | ||
1228 | "host claims to use CS GPIOs but no CS found in DT by the SPI core\n"); | ||
1229 | } | ||
1230 | asd->npcs_pin = spi->cs_gpiod; | ||
1231 | } | ||
1237 | 1232 | ||
1238 | asd->npcs_pin = npcs_pin; | ||
1239 | spi->controller_state = asd; | 1233 | spi->controller_state = asd; |
1240 | } | 1234 | } |
1241 | 1235 | ||
@@ -1473,41 +1467,6 @@ static void atmel_get_caps(struct atmel_spi *as) | |||
1473 | as->caps.has_pdc_support = version < 0x212; | 1467 | as->caps.has_pdc_support = version < 0x212; |
1474 | } | 1468 | } |
1475 | 1469 | ||
1476 | /*-------------------------------------------------------------------------*/ | ||
1477 | static int atmel_spi_gpio_cs(struct platform_device *pdev) | ||
1478 | { | ||
1479 | struct spi_master *master = platform_get_drvdata(pdev); | ||
1480 | struct atmel_spi *as = spi_master_get_devdata(master); | ||
1481 | struct device_node *np = master->dev.of_node; | ||
1482 | int i; | ||
1483 | int ret = 0; | ||
1484 | int nb = 0; | ||
1485 | |||
1486 | if (!as->use_cs_gpios) | ||
1487 | return 0; | ||
1488 | |||
1489 | if (!np) | ||
1490 | return 0; | ||
1491 | |||
1492 | nb = of_gpio_named_count(np, "cs-gpios"); | ||
1493 | for (i = 0; i < nb; i++) { | ||
1494 | int cs_gpio = of_get_named_gpio(pdev->dev.of_node, | ||
1495 | "cs-gpios", i); | ||
1496 | |||
1497 | if (cs_gpio == -EPROBE_DEFER) | ||
1498 | return cs_gpio; | ||
1499 | |||
1500 | if (gpio_is_valid(cs_gpio)) { | ||
1501 | ret = devm_gpio_request(&pdev->dev, cs_gpio, | ||
1502 | dev_name(&pdev->dev)); | ||
1503 | if (ret) | ||
1504 | return ret; | ||
1505 | } | ||
1506 | } | ||
1507 | |||
1508 | return 0; | ||
1509 | } | ||
1510 | |||
1511 | static void atmel_spi_init(struct atmel_spi *as) | 1470 | static void atmel_spi_init(struct atmel_spi *as) |
1512 | { | 1471 | { |
1513 | spi_writel(as, CR, SPI_BIT(SWRST)); | 1472 | spi_writel(as, CR, SPI_BIT(SWRST)); |
@@ -1560,6 +1519,7 @@ static int atmel_spi_probe(struct platform_device *pdev) | |||
1560 | goto out_free; | 1519 | goto out_free; |
1561 | 1520 | ||
1562 | /* the spi->mode bits understood by this driver: */ | 1521 | /* the spi->mode bits understood by this driver: */ |
1522 | master->use_gpio_descriptors = true; | ||
1563 | master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH; | 1523 | master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH; |
1564 | master->bits_per_word_mask = SPI_BPW_RANGE_MASK(8, 16); | 1524 | master->bits_per_word_mask = SPI_BPW_RANGE_MASK(8, 16); |
1565 | master->dev.of_node = pdev->dev.of_node; | 1525 | master->dev.of_node = pdev->dev.of_node; |
@@ -1592,6 +1552,11 @@ static int atmel_spi_probe(struct platform_device *pdev) | |||
1592 | 1552 | ||
1593 | atmel_get_caps(as); | 1553 | atmel_get_caps(as); |
1594 | 1554 | ||
1555 | /* | ||
1556 | * If there are chip selects in the device tree, those will be | ||
1557 | * discovered by the SPI core when registering the SPI master | ||
1558 | * and assigned to each SPI device. | ||
1559 | */ | ||
1595 | as->use_cs_gpios = true; | 1560 | as->use_cs_gpios = true; |
1596 | if (atmel_spi_is_v2(as) && | 1561 | if (atmel_spi_is_v2(as) && |
1597 | pdev->dev.of_node && | 1562 | pdev->dev.of_node && |
@@ -1600,10 +1565,6 @@ static int atmel_spi_probe(struct platform_device *pdev) | |||
1600 | master->num_chipselect = 4; | 1565 | master->num_chipselect = 4; |
1601 | } | 1566 | } |
1602 | 1567 | ||
1603 | ret = atmel_spi_gpio_cs(pdev); | ||
1604 | if (ret) | ||
1605 | goto out_unmap_regs; | ||
1606 | |||
1607 | as->use_dma = false; | 1568 | as->use_dma = false; |
1608 | as->use_pdc = false; | 1569 | as->use_pdc = false; |
1609 | if (as->caps.has_dma_support) { | 1570 | if (as->caps.has_dma_support) { |