Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'spi-fix-v5.9-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi

Pull spi fixes from Mark Brown:
"A bunch of fixes that came in for SPI during the merge window.

Some from ST and others for their controller, one from Lukas for a
race between device addition and controller unregistration and one
from fix from Geert for the DT bindings which unbreaks validation"

* tag 'spi-fix-v5.9-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi:
dt-bindings: lpspi: Add missing boolean type for fsl,spi-only-use-cs1-sel
spi: stm32: always perform registers configuration prior to transfer
spi: stm32: fixes suspend/resume management
spi: stm32: fix stm32_spi_prepare_mbr in case of odd clk_rate
spi: stm32: fix fifo threshold level in case of short transfer
spi: stm32h7: fix race condition at end of transfer
spi: stm32: clear only asserted irq flags on interrupt
spi: Prevent adding devices below an unregistering controller

+89 -42
+1
Documentation/devicetree/bindings/spi/spi-fsl-lpspi.yaml
··· 39 39 spi common code does not support use of CS signals discontinuously. 40 40 i.MX8DXL-EVK board only uses CS1 without using CS0. Therefore, add 41 41 this property to re-config the chipselect value in the LPSPI driver. 42 + type: boolean 42 43 43 44 required: 44 45 - compatible
+3
drivers/spi/Kconfig
··· 1017 1017 1018 1018 endif # SPI_SLAVE 1019 1019 1020 + config SPI_DYNAMIC 1021 + def_bool ACPI || OF_DYNAMIC || SPI_SLAVE 1022 + 1020 1023 endif # SPI
+65 -41
drivers/spi/spi-stm32.c
··· 13 13 #include <linux/iopoll.h> 14 14 #include <linux/module.h> 15 15 #include <linux/of_platform.h> 16 + #include <linux/pinctrl/consumer.h> 16 17 #include <linux/pm_runtime.h> 17 18 #include <linux/reset.h> 18 19 #include <linux/spi/spi.h> ··· 442 441 { 443 442 u32 div, mbrdiv; 444 443 445 - div = DIV_ROUND_UP(spi->clk_rate, speed_hz); 444 + /* Ensure spi->clk_rate is even */ 445 + div = DIV_ROUND_UP(spi->clk_rate & ~0x1, speed_hz); 446 446 447 447 /* 448 448 * SPI framework set xfer->speed_hz to master->max_speed_hz if ··· 469 467 /** 470 468 * stm32h7_spi_prepare_fthlv - Determine FIFO threshold level 471 469 * @spi: pointer to the spi controller data structure 470 + * @xfer_len: length of the message to be transferred 472 471 */ 473 - static u32 stm32h7_spi_prepare_fthlv(struct stm32_spi *spi) 472 + static u32 stm32h7_spi_prepare_fthlv(struct stm32_spi *spi, u32 xfer_len) 474 473 { 475 - u32 fthlv, half_fifo; 474 + u32 fthlv, half_fifo, packet; 476 475 477 476 /* data packet should not exceed 1/2 of fifo space */ 478 477 half_fifo = (spi->fifo_size / 2); 479 478 480 - if (spi->cur_bpw <= 8) 481 - fthlv = half_fifo; 482 - else if (spi->cur_bpw <= 16) 483 - fthlv = half_fifo / 2; 479 + /* data_packet should not exceed transfer length */ 480 + if (half_fifo > xfer_len) 481 + packet = xfer_len; 484 482 else 485 - fthlv = half_fifo / 4; 483 + packet = half_fifo; 484 + 485 + if (spi->cur_bpw <= 8) 486 + fthlv = packet; 487 + else if (spi->cur_bpw <= 16) 488 + fthlv = packet / 2; 489 + else 490 + fthlv = packet / 4; 486 491 487 492 /* align packet size with data registers access */ 488 493 if (spi->cur_bpw > 8) 489 494 fthlv -= (fthlv % 2); /* multiple of 2 */ 490 495 else 491 496 fthlv -= (fthlv % 4); /* multiple of 4 */ 497 + 498 + if (!fthlv) 499 + fthlv = 1; 492 500 493 501 return fthlv; 494 502 } ··· 978 966 if (!spi->cur_usedma && (spi->rx_buf && (spi->rx_len > 0))) 979 967 stm32h7_spi_read_rxfifo(spi, false); 980 968 981 - writel_relaxed(mask, spi->base + STM32H7_SPI_IFCR); 969 + writel_relaxed(sr & mask, spi->base + STM32H7_SPI_IFCR); 982 970 983 971 spin_unlock_irqrestore(&spi->lock, flags); 984 972 985 973 if (end) { 986 - spi_finalize_current_transfer(master); 987 974 stm32h7_spi_disable(spi); 975 + spi_finalize_current_transfer(master); 988 976 } 989 977 990 978 return IRQ_HANDLED; ··· 1405 1393 cfg1_setb |= (bpw << STM32H7_SPI_CFG1_DSIZE_SHIFT) & 1406 1394 STM32H7_SPI_CFG1_DSIZE; 1407 1395 1408 - spi->cur_fthlv = stm32h7_spi_prepare_fthlv(spi); 1396 + spi->cur_fthlv = stm32h7_spi_prepare_fthlv(spi, spi->cur_xferlen); 1409 1397 fthlv = spi->cur_fthlv - 1; 1410 1398 1411 1399 cfg1_clrb |= STM32H7_SPI_CFG1_FTHLV; ··· 1597 1585 unsigned long flags; 1598 1586 unsigned int comm_type; 1599 1587 int nb_words, ret = 0; 1588 + int mbr; 1600 1589 1601 1590 spin_lock_irqsave(&spi->lock, flags); 1602 1591 1603 - if (spi->cur_bpw != transfer->bits_per_word) { 1604 - spi->cur_bpw = transfer->bits_per_word; 1605 - spi->cfg->set_bpw(spi); 1592 + spi->cur_xferlen = transfer->len; 1593 + 1594 + spi->cur_bpw = transfer->bits_per_word; 1595 + spi->cfg->set_bpw(spi); 1596 + 1597 + /* Update spi->cur_speed with real clock speed */ 1598 + mbr = stm32_spi_prepare_mbr(spi, transfer->speed_hz, 1599 + spi->cfg->baud_rate_div_min, 1600 + spi->cfg->baud_rate_div_max); 1601 + if (mbr < 0) { 1602 + ret = mbr; 1603 + goto out; 1606 1604 } 1607 1605 1608 - if (spi->cur_speed != transfer->speed_hz) { 1609 - int mbr; 1610 - 1611 - /* Update spi->cur_speed with real clock speed */ 1612 - mbr = stm32_spi_prepare_mbr(spi, transfer->speed_hz, 1613 - spi->cfg->baud_rate_div_min, 1614 - spi->cfg->baud_rate_div_max); 1615 - if (mbr < 0) { 1616 - ret = mbr; 1617 - goto out; 1618 - } 1619 - 1620 - transfer->speed_hz = spi->cur_speed; 1621 - stm32_spi_set_mbr(spi, mbr); 1622 - } 1606 + transfer->speed_hz = spi->cur_speed; 1607 + stm32_spi_set_mbr(spi, mbr); 1623 1608 1624 1609 comm_type = stm32_spi_communication_type(spi_dev, transfer); 1625 - if (spi->cur_comm != comm_type) { 1626 - ret = spi->cfg->set_mode(spi, comm_type); 1610 + ret = spi->cfg->set_mode(spi, comm_type); 1611 + if (ret < 0) 1612 + goto out; 1627 1613 1628 - if (ret < 0) 1629 - goto out; 1630 - 1631 - spi->cur_comm = comm_type; 1632 - } 1614 + spi->cur_comm = comm_type; 1633 1615 1634 1616 if (spi->cfg->set_data_idleness) 1635 1617 spi->cfg->set_data_idleness(spi, transfer->len); ··· 1640 1634 if (ret < 0) 1641 1635 goto out; 1642 1636 } 1643 - 1644 - spi->cur_xferlen = transfer->len; 1645 1637 1646 1638 dev_dbg(spi->dev, "transfer communication mode set to %d\n", 1647 1639 spi->cur_comm); ··· 2000 1996 2001 1997 pm_runtime_disable(&pdev->dev); 2002 1998 1999 + pinctrl_pm_select_sleep_state(&pdev->dev); 2000 + 2003 2001 return 0; 2004 2002 } 2005 2003 ··· 2013 2007 2014 2008 clk_disable_unprepare(spi->clk); 2015 2009 2016 - return 0; 2010 + return pinctrl_pm_select_sleep_state(dev); 2017 2011 } 2018 2012 2019 2013 static int stm32_spi_runtime_resume(struct device *dev) 2020 2014 { 2021 2015 struct spi_master *master = dev_get_drvdata(dev); 2022 2016 struct stm32_spi *spi = spi_master_get_devdata(master); 2017 + int ret; 2018 + 2019 + ret = pinctrl_pm_select_default_state(dev); 2020 + if (ret) 2021 + return ret; 2023 2022 2024 2023 return clk_prepare_enable(spi->clk); 2025 2024 } ··· 2054 2043 return ret; 2055 2044 2056 2045 ret = spi_master_resume(master); 2057 - if (ret) 2046 + if (ret) { 2058 2047 clk_disable_unprepare(spi->clk); 2048 + return ret; 2049 + } 2059 2050 2060 - return ret; 2051 + ret = pm_runtime_get_sync(dev); 2052 + if (ret) { 2053 + dev_err(dev, "Unable to power device:%d\n", ret); 2054 + return ret; 2055 + } 2056 + 2057 + spi->cfg->config(spi); 2058 + 2059 + pm_runtime_mark_last_busy(dev); 2060 + pm_runtime_put_autosuspend(dev); 2061 + 2062 + return 0; 2061 2063 } 2062 2064 #endif 2063 2065
+20 -1
drivers/spi/spi.c
··· 475 475 */ 476 476 static DEFINE_MUTEX(board_lock); 477 477 478 + /* 479 + * Prevents addition of devices with same chip select and 480 + * addition of devices below an unregistering controller. 481 + */ 482 + static DEFINE_MUTEX(spi_add_lock); 483 + 478 484 /** 479 485 * spi_alloc_device - Allocate a new SPI device 480 486 * @ctlr: Controller to which device is connected ··· 560 554 */ 561 555 int spi_add_device(struct spi_device *spi) 562 556 { 563 - static DEFINE_MUTEX(spi_add_lock); 564 557 struct spi_controller *ctlr = spi->controller; 565 558 struct device *dev = ctlr->dev.parent; 566 559 int status; ··· 584 579 if (status) { 585 580 dev_err(dev, "chipselect %d already in use\n", 586 581 spi->chip_select); 582 + goto done; 583 + } 584 + 585 + /* Controller may unregister concurrently */ 586 + if (IS_ENABLED(CONFIG_SPI_DYNAMIC) && 587 + !device_is_registered(&ctlr->dev)) { 588 + status = -ENODEV; 587 589 goto done; 588 590 } 589 591 ··· 2807 2795 struct spi_controller *found; 2808 2796 int id = ctlr->bus_num; 2809 2797 2798 + /* Prevent addition of new devices, unregister existing ones */ 2799 + if (IS_ENABLED(CONFIG_SPI_DYNAMIC)) 2800 + mutex_lock(&spi_add_lock); 2801 + 2810 2802 device_for_each_child(&ctlr->dev, NULL, __unregister); 2811 2803 2812 2804 /* First make sure that this controller was ever added */ ··· 2831 2815 if (found == ctlr) 2832 2816 idr_remove(&spi_master_idr, id); 2833 2817 mutex_unlock(&board_lock); 2818 + 2819 + if (IS_ENABLED(CONFIG_SPI_DYNAMIC)) 2820 + mutex_unlock(&spi_add_lock); 2834 2821 } 2835 2822 EXPORT_SYMBOL_GPL(spi_unregister_controller); 2836 2823