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

spi: stm32: introduction of stm32h7 SPI device mode support

Add support for stm32h7 to use SPI controller in device role.
In such case, the spi instance should have the spi-slave property
defined.

Signed-off-by: Alain Volmat <alain.volmat@foss.st.com>
Signed-off-by: Valentin Caron <valentin.caron@foss.st.com>
Link: https://lore.kernel.org/r/20230615075815.310261-5-valentin.caron@foss.st.com
Signed-off-by: Mark Brown <broonie@kernel.org>

authored by

Valentin Caron and committed by
Mark Brown
e40335fc 4f2b39dc

+79 -34
+1
drivers/spi/Kconfig
··· 936 936 config SPI_STM32 937 937 tristate "STMicroelectronics STM32 SPI controller" 938 938 depends on ARCH_STM32 || COMPILE_TEST 939 + select SPI_SLAVE 939 940 help 940 941 SPI driver for STMicroelectronics STM32 SoCs. 941 942
+78 -34
drivers/spi/spi-stm32.c
··· 117 117 #define STM32H7_SPI_CFG2_CPHA BIT(24) 118 118 #define STM32H7_SPI_CFG2_CPOL BIT(25) 119 119 #define STM32H7_SPI_CFG2_SSM BIT(26) 120 + #define STM32H7_SPI_CFG2_SSIOP BIT(28) 120 121 #define STM32H7_SPI_CFG2_AFCNTR BIT(31) 121 122 122 123 /* STM32H7_SPI_IER bit fields */ ··· 171 170 */ 172 171 #define SPI_DMA_MIN_BYTES 16 173 172 173 + /* STM32 SPI driver helpers */ 174 + #define STM32_SPI_MASTER_MODE(stm32_spi) (!(stm32_spi)->device_mode) 175 + #define STM32_SPI_DEVICE_MODE(stm32_spi) ((stm32_spi)->device_mode) 176 + 174 177 /** 175 178 * struct stm32_spi_reg - stm32 SPI register & bitfield desc 176 179 * @reg: register offset ··· 195 190 * @cpol: clock polarity register and polarity bit 196 191 * @cpha: clock phase register and phase bit 197 192 * @lsb_first: LSB transmitted first register and bit 193 + * @cs_high: chips select active value 198 194 * @br: baud rate register and bitfields 199 195 * @rx: SPI RX data register 200 196 * @tx: SPI TX data register ··· 207 201 const struct stm32_spi_reg cpol; 208 202 const struct stm32_spi_reg cpha; 209 203 const struct stm32_spi_reg lsb_first; 204 + const struct stm32_spi_reg cs_high; 210 205 const struct stm32_spi_reg br; 211 206 const struct stm32_spi_reg rx; 212 207 const struct stm32_spi_reg tx; ··· 287 280 * @dma_tx: dma channel for TX transfer 288 281 * @dma_rx: dma channel for RX transfer 289 282 * @phys_addr: SPI registers physical base address 283 + * @device_mode: the controller is configured as SPI device 290 284 */ 291 285 struct stm32_spi { 292 286 struct device *dev; ··· 315 307 struct dma_chan *dma_tx; 316 308 struct dma_chan *dma_rx; 317 309 dma_addr_t phys_addr; 310 + 311 + bool device_mode; 318 312 }; 319 313 320 314 static const struct stm32_spi_regspec stm32f4_spi_regspec = { ··· 328 318 .cpol = { STM32F4_SPI_CR1, STM32F4_SPI_CR1_CPOL }, 329 319 .cpha = { STM32F4_SPI_CR1, STM32F4_SPI_CR1_CPHA }, 330 320 .lsb_first = { STM32F4_SPI_CR1, STM32F4_SPI_CR1_LSBFRST }, 321 + .cs_high = {}, 331 322 .br = { STM32F4_SPI_CR1, STM32F4_SPI_CR1_BR, STM32F4_SPI_CR1_BR_SHIFT }, 332 323 333 324 .rx = { STM32F4_SPI_DR }, ··· 347 336 .cpol = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_CPOL }, 348 337 .cpha = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_CPHA }, 349 338 .lsb_first = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_LSBFRST }, 339 + .cs_high = { STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_SSIOP }, 350 340 .br = { STM32H7_SPI_CFG1, STM32H7_SPI_CFG1_MBR, 351 341 STM32H7_SPI_CFG1_MBR_SHIFT }, 352 342 ··· 983 971 else 984 972 clrb |= spi->cfg->regs->lsb_first.mask; 985 973 974 + if (STM32_SPI_DEVICE_MODE(spi) && spi_dev->mode & SPI_CS_HIGH) 975 + setb |= spi->cfg->regs->cs_high.mask; 976 + else 977 + clrb |= spi->cfg->regs->cs_high.mask; 978 + 986 979 dev_dbg(spi->dev, "cpol=%d cpha=%d lsb_first=%d cs_high=%d\n", 987 980 !!(spi_dev->mode & SPI_CPOL), 988 981 !!(spi_dev->mode & SPI_CPHA), ··· 1178 1161 if (spi->tx_buf) 1179 1162 stm32h7_spi_write_txfifo(spi); 1180 1163 1181 - stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_CSTART); 1164 + if (STM32_SPI_MASTER_MODE(spi)) 1165 + stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_CSTART); 1182 1166 1183 1167 writel_relaxed(ier, spi->base + STM32H7_SPI_IER); 1184 1168 ··· 1226 1208 1227 1209 stm32_spi_enable(spi); 1228 1210 1229 - stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_CSTART); 1211 + if (STM32_SPI_MASTER_MODE(spi)) 1212 + stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_CSTART); 1230 1213 } 1231 1214 1232 1215 /** ··· 1555 1536 spi->cfg->set_bpw(spi); 1556 1537 1557 1538 /* Update spi->cur_speed with real clock speed */ 1558 - mbr = stm32_spi_prepare_mbr(spi, transfer->speed_hz, 1559 - spi->cfg->baud_rate_div_min, 1560 - spi->cfg->baud_rate_div_max); 1561 - if (mbr < 0) { 1562 - ret = mbr; 1563 - goto out; 1564 - } 1539 + if (STM32_SPI_MASTER_MODE(spi)) { 1540 + mbr = stm32_spi_prepare_mbr(spi, transfer->speed_hz, 1541 + spi->cfg->baud_rate_div_min, 1542 + spi->cfg->baud_rate_div_max); 1543 + if (mbr < 0) { 1544 + ret = mbr; 1545 + goto out; 1546 + } 1565 1547 1566 - transfer->speed_hz = spi->cur_speed; 1567 - stm32_spi_set_mbr(spi, mbr); 1548 + transfer->speed_hz = spi->cur_speed; 1549 + stm32_spi_set_mbr(spi, mbr); 1550 + } 1568 1551 1569 1552 comm_type = stm32_spi_communication_type(spi_dev, transfer); 1570 1553 ret = spi->cfg->set_mode(spi, comm_type); ··· 1575 1554 1576 1555 spi->cur_comm = comm_type; 1577 1556 1578 - if (spi->cfg->set_data_idleness) 1557 + if (STM32_SPI_MASTER_MODE(spi) && spi->cfg->set_data_idleness) 1579 1558 spi->cfg->set_data_idleness(spi, transfer->len); 1580 1559 1581 1560 if (spi->cur_bpw <= 8) ··· 1596 1575 dev_dbg(spi->dev, 1597 1576 "data frame of %d-bit, data packet of %d data frames\n", 1598 1577 spi->cur_bpw, spi->cur_fthlv); 1599 - dev_dbg(spi->dev, "speed set to %dHz\n", spi->cur_speed); 1578 + if (STM32_SPI_MASTER_MODE(spi)) 1579 + dev_dbg(spi->dev, "speed set to %dHz\n", spi->cur_speed); 1600 1580 dev_dbg(spi->dev, "transfer of %d bytes (%d data frames)\n", 1601 1581 spi->cur_xferlen, nb_words); 1602 1582 dev_dbg(spi->dev, "dma %s\n", ··· 1692 1670 } 1693 1671 1694 1672 /** 1695 - * stm32h7_spi_config - Configure SPI controller as SPI master 1673 + * stm32h7_spi_config - Configure SPI controller 1696 1674 * @spi: pointer to the spi controller data structure 1697 1675 */ 1698 1676 static int stm32h7_spi_config(struct stm32_spi *spi) 1699 1677 { 1700 1678 unsigned long flags; 1679 + u32 cr1 = 0, cfg2 = 0; 1701 1680 1702 1681 spin_lock_irqsave(&spi->lock, flags); 1703 1682 ··· 1706 1683 stm32_spi_clr_bits(spi, STM32H7_SPI_I2SCFGR, 1707 1684 STM32H7_SPI_I2SCFGR_I2SMOD); 1708 1685 1709 - /* 1710 - * - SS input value high 1711 - * - transmitter half duplex direction 1712 - * - automatic communication suspend when RX-Fifo is full 1713 - */ 1714 - stm32_spi_set_bits(spi, STM32H7_SPI_CR1, STM32H7_SPI_CR1_SSI | 1715 - STM32H7_SPI_CR1_HDDIR | 1716 - STM32H7_SPI_CR1_MASRX); 1686 + if (STM32_SPI_DEVICE_MODE(spi)) { 1687 + /* Use native device select */ 1688 + cfg2 &= ~STM32H7_SPI_CFG2_SSM; 1689 + } else { 1690 + /* 1691 + * - Transmitter half duplex direction 1692 + * - Automatic communication suspend when RX-Fifo is full 1693 + * - SS input value high 1694 + */ 1695 + cr1 |= STM32H7_SPI_CR1_HDDIR | STM32H7_SPI_CR1_MASRX | STM32H7_SPI_CR1_SSI; 1717 1696 1718 - /* 1719 - * - Set the master mode (default Motorola mode) 1720 - * - Consider 1 master/n slaves configuration and 1721 - * SS input value is determined by the SSI bit 1722 - * - keep control of all associated GPIOs 1723 - */ 1724 - stm32_spi_set_bits(spi, STM32H7_SPI_CFG2, STM32H7_SPI_CFG2_MASTER | 1725 - STM32H7_SPI_CFG2_SSM | 1726 - STM32H7_SPI_CFG2_AFCNTR); 1697 + /* 1698 + * - Set the master mode (default Motorola mode) 1699 + * - Consider 1 master/n devices configuration and 1700 + * SS input value is determined by the SSI bit 1701 + * - keep control of all associated GPIOs 1702 + */ 1703 + cfg2 |= STM32H7_SPI_CFG2_MASTER | STM32H7_SPI_CFG2_SSM | STM32H7_SPI_CFG2_AFCNTR; 1704 + } 1705 + 1706 + stm32_spi_set_bits(spi, STM32H7_SPI_CR1, cr1); 1707 + stm32_spi_set_bits(spi, STM32H7_SPI_CFG2, cfg2); 1727 1708 1728 1709 spin_unlock_irqrestore(&spi->lock, flags); 1729 1710 ··· 1783 1756 }; 1784 1757 MODULE_DEVICE_TABLE(of, stm32_spi_of_match); 1785 1758 1759 + static int stm32h7_spi_device_abort(struct spi_controller *ctrl) 1760 + { 1761 + spi_finalize_current_transfer(ctrl); 1762 + return 0; 1763 + } 1764 + 1786 1765 static int stm32_spi_probe(struct platform_device *pdev) 1787 1766 { 1788 1767 struct spi_controller *ctrl; 1789 1768 struct stm32_spi *spi; 1790 1769 struct resource *res; 1791 1770 struct reset_control *rst; 1771 + struct device_node *np = pdev->dev.of_node; 1772 + bool device_mode; 1792 1773 int ret; 1793 1774 1794 - ctrl = devm_spi_alloc_master(&pdev->dev, sizeof(struct stm32_spi)); 1775 + device_mode = of_property_read_bool(np, "spi-slave"); 1776 + 1777 + if (device_mode) 1778 + ctrl = devm_spi_alloc_slave(&pdev->dev, sizeof(struct stm32_spi)); 1779 + else 1780 + ctrl = devm_spi_alloc_master(&pdev->dev, sizeof(struct stm32_spi)); 1795 1781 if (!ctrl) { 1796 - dev_err(&pdev->dev, "spi master allocation failed\n"); 1782 + dev_err(&pdev->dev, "spi controller allocation failed\n"); 1797 1783 return -ENOMEM; 1798 1784 } 1799 1785 platform_set_drvdata(pdev, ctrl); ··· 1814 1774 spi = spi_controller_get_devdata(ctrl); 1815 1775 spi->dev = &pdev->dev; 1816 1776 spi->ctrl = ctrl; 1777 + spi->device_mode = device_mode; 1817 1778 spin_lock_init(&spi->lock); 1818 1779 1819 1780 spi->cfg = (const struct stm32_spi_cfg *) ··· 1897 1856 ctrl->transfer_one = stm32_spi_transfer_one; 1898 1857 ctrl->unprepare_message = stm32_spi_unprepare_msg; 1899 1858 ctrl->flags = spi->cfg->flags; 1859 + if (STM32_SPI_DEVICE_MODE(spi)) 1860 + ctrl->slave_abort = stm32h7_spi_device_abort; 1900 1861 1901 1862 spi->dma_tx = dma_request_chan(spi->dev, "tx"); 1902 1863 if (IS_ERR(spi->dma_tx)) { ··· 1944 1901 pm_runtime_mark_last_busy(&pdev->dev); 1945 1902 pm_runtime_put_autosuspend(&pdev->dev); 1946 1903 1947 - dev_info(&pdev->dev, "driver initialized\n"); 1904 + dev_info(&pdev->dev, "driver initialized (%s mode)\n", 1905 + STM32_SPI_MASTER_MODE(spi) ? "master" : "device"); 1948 1906 1949 1907 return 0; 1950 1908