2023-05-30 14:26:11

by Boerge Struempfel

[permalink] [raw]
Subject: [PATCH v7 0/5] spi: add SPI_MOSI_IDLE_LOW mode bit

Some spi controller switch the mosi line to high, whenever they are
idle. This may not be desired in all use cases. For example neopixel
leds can get confused and flicker due to misinterpreting the idle state.
Therefore, we introduce a new spi-mode bit, with which the idle behaviour
can be overwritten on a per device basis.

---

Link for versions:
v1 and v2: https://lore.kernel.org/linux-spi/[email protected]/
v3: https://lore.kernel.org/linux-spi/[email protected]/T/#t
v4: https://lore.kernel.org/linux-spi/CAEktqctboF3=ykVNtPsifcmHzF6dWwoEcVh+O4H1u-R=TT6gHg@mail.gmail.com/T/#t
v5: https://lore.kernel.org/linux-spi/[email protected]/T/#t
v6: https://lore.kernel.org/linux-spi/[email protected]/T/#t

Changes from V6:
- Adjusted the patches for next/master

Changes from V5:
- Added a patch to reorder the command line options for
spidev_test in order to increase usability. The indentation
fixes were also done in this patch.

Changes from V4:
- Added the SPI_3WIRE_HIZ mode bit to spidev
- Added the SPI_MOSI_IDLE_LOW mode bit to the spidev_test
userspace tool and added the two other missing spi mode
bits (SPI_3WIRE_HIZ and SPI_RX_CPHA_FLIP) to it as well.

Changes from V3:
- Added missing paranthesis which caused builderrors

Changes from V2:
- Removed the device-tree binding since this should not be managed by
the DT but by the device itself.
- Replaced all occurences of spi->chip_select with the corresponding
macro spi_get_chipselect(spi,0)

Changes from V1:
- Added patch, introducing the new devicetree binding flag
- Split the generic spi part of the patch from the imx-spi specific
part
- Replaced SPI_CPOL and SPI_CPHA by the combined SPI_MODE_X_MASK bit
in the imx-spi.c modebits.
- Added the SPI_MOSI_IDLE_LOW bit to spidev

Boerge Struempfel (5):
spi: add SPI_MOSI_IDLE_LOW mode bit
spi: spi-imx: add support for SPI_MOSI_IDLE_LOW mode bit
spi: spidev: add two new spi mode bits
spi: spidev_test: Sorted the options into logical groups
spi: spidev_test Add three missing spi mode bits

drivers/spi/spi-imx.c | 9 ++-
drivers/spi/spidev.c | 3 +-
include/uapi/linux/spi/spi.h | 3 +-
tools/spi/spidev_test.c | 107 +++++++++++++++++++++--------------
4 files changed, 76 insertions(+), 46 deletions(-)

--
2.40.1



2023-05-30 14:26:42

by Boerge Struempfel

[permalink] [raw]
Subject: [PATCH v7 4/5] spi: spidev_test: Sorted the options into logical groups

In order to increase usability, the command line options are sorted into
logical groups. In addition, the usage string was sorted alphabetically,
and the missing parameters '8','i' and 'o' were added. Furthermore, the
option descriptions were moved further to the right, in order to allow
for longer option names.

Signed-off-by: Boerge Struempfel <[email protected]>
---
tools/spi/spidev_test.c | 90 ++++++++++++++++++++++-------------------
1 file changed, 48 insertions(+), 42 deletions(-)

diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c
index b0ca44c70e83..2d2cee339b39 100644
--- a/tools/spi/spidev_test.c
+++ b/tools/spi/spidev_test.c
@@ -172,28 +172,34 @@

static void print_usage(const char *prog)
{
- printf("Usage: %s [-DsbdlHOLC3vpNR24SI]\n", prog);
- puts(" -D --device device to use (default /dev/spidev1.1)\n"
- " -s --speed max speed (Hz)\n"
- " -d --delay delay (usec)\n"
- " -b --bpw bits per word\n"
- " -i --input input data from a file (e.g. \"test.bin\")\n"
- " -o --output output data to a file (e.g. \"results.bin\")\n"
- " -l --loop loopback\n"
- " -H --cpha clock phase\n"
- " -O --cpol clock polarity\n"
- " -L --lsb least significant bit first\n"
- " -C --cs-high chip select active high\n"
- " -3 --3wire SI/SO signals shared\n"
- " -v --verbose Verbose (show tx buffer)\n"
- " -p Send data (e.g. \"1234\\xde\\xad\")\n"
- " -N --no-cs no chip select\n"
- " -R --ready slave pulls low to pause\n"
- " -2 --dual dual transfer\n"
- " -4 --quad quad transfer\n"
- " -8 --octal octal transfer\n"
- " -S --size transfer size\n"
- " -I --iter iterations\n");
+ printf("Usage: %s [-2348CDHILNORSbdilopsv]\n", prog);
+ puts("general device settings:\n"
+ " -D --device device to use (default /dev/spidev1.1)\n"
+ " -s --speed max speed (Hz)\n"
+ " -d --delay delay (usec)\n"
+ " -l --loop loopback\n"
+ "spi mode:\n"
+ " -H --cpha clock phase\n"
+ " -O --cpol clock polarity\n"
+ "number of wires for transmission:\n"
+ " -2 --dual dual transfer\n"
+ " -4 --quad quad transfer\n"
+ " -8 --octal octal transfer\n"
+ " -3 --3wire SI/SO signals shared\n"
+ "data:\n"
+ " -i --input input data from a file (e.g. \"test.bin\")\n"
+ " -o --output output data to a file (e.g. \"results.bin\")\n"
+ " -p Send data (e.g. \"1234\\xde\\xad\")\n"
+ " -S --size transfer size\n"
+ " -I --iter iterations\n"
+ "additional parameters:\n"
+ " -b --bpw bits per word\n"
+ " -L --lsb least significant bit first\n"
+ " -C --cs-high chip select active high\n"
+ " -N --no-cs no chip select\n"
+ " -R --ready slave pulls low to pause\n"
+ "misc:\n"
+ " -v --verbose Verbose (show tx buffer)\n");
exit(1);
}

@@ -201,26 +207,26 @@ static void parse_opts(int argc, char *argv[])
{
while (1) {
static const struct option lopts[] = {
- { "device", 1, 0, 'D' },
- { "speed", 1, 0, 's' },
- { "delay", 1, 0, 'd' },
- { "bpw", 1, 0, 'b' },
- { "input", 1, 0, 'i' },
- { "output", 1, 0, 'o' },
- { "loop", 0, 0, 'l' },
- { "cpha", 0, 0, 'H' },
- { "cpol", 0, 0, 'O' },
- { "lsb", 0, 0, 'L' },
- { "cs-high", 0, 0, 'C' },
- { "3wire", 0, 0, '3' },
- { "no-cs", 0, 0, 'N' },
- { "ready", 0, 0, 'R' },
- { "dual", 0, 0, '2' },
- { "verbose", 0, 0, 'v' },
- { "quad", 0, 0, '4' },
- { "octal", 0, 0, '8' },
- { "size", 1, 0, 'S' },
- { "iter", 1, 0, 'I' },
+ { "device", 1, 0, 'D' },
+ { "speed", 1, 0, 's' },
+ { "delay", 1, 0, 'd' },
+ { "loop", 0, 0, 'l' },
+ { "cpha", 0, 0, 'H' },
+ { "cpol", 0, 0, 'O' },
+ { "dual", 0, 0, '2' },
+ { "quad", 0, 0, '4' },
+ { "octal", 0, 0, '8' },
+ { "3wire", 0, 0, '3' },
+ { "input", 1, 0, 'i' },
+ { "output", 1, 0, 'o' },
+ { "size", 1, 0, 'S' },
+ { "iter", 1, 0, 'I' },
+ { "bpw", 1, 0, 'b' },
+ { "lsb", 0, 0, 'L' },
+ { "cs-high", 0, 0, 'C' },
+ { "no-cs", 0, 0, 'N' },
+ { "ready", 0, 0, 'R' },
+ { "verbose", 0, 0, 'v' },
{ NULL, 0, 0, 0 },
};
int c;
--
2.40.1


2023-05-30 14:26:53

by Boerge Struempfel

[permalink] [raw]
Subject: [PATCH v7 1/5] spi: add SPI_MOSI_IDLE_LOW mode bit

Some spi controller switch the mosi line to high, whenever they are
idle. This may not be desired in all use cases. For example neopixel
leds can get confused and flicker due to misinterpreting the idle state.
Therefore, we introduce a new spi-mode bit, with which the idle behaviour
can be overwritten on a per device basis.

Signed-off-by: Boerge Struempfel <[email protected]>
---
include/uapi/linux/spi/spi.h | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/include/uapi/linux/spi/spi.h b/include/uapi/linux/spi/spi.h
index 9d5f58059703..ca56e477d161 100644
--- a/include/uapi/linux/spi/spi.h
+++ b/include/uapi/linux/spi/spi.h
@@ -28,6 +28,7 @@
#define SPI_RX_OCTAL _BITUL(14) /* receive with 8 wires */
#define SPI_3WIRE_HIZ _BITUL(15) /* high impedance turnaround */
#define SPI_RX_CPHA_FLIP _BITUL(16) /* flip CPHA on Rx only xfer */
+#define SPI_MOSI_IDLE_LOW _BITUL(17) /* leave mosi line low when idle */

/*
* All the bits defined above should be covered by SPI_MODE_USER_MASK.
@@ -37,6 +38,6 @@
* These bits must not overlap. A static assert check should make sure of that.
* If adding extra bits, make sure to increase the bit index below as well.
*/
-#define SPI_MODE_USER_MASK (_BITUL(17) - 1)
+#define SPI_MODE_USER_MASK (_BITUL(18) - 1)

#endif /* _UAPI_SPI_H */
--
2.40.1


2023-05-30 14:40:24

by Boerge Struempfel

[permalink] [raw]
Subject: [PATCH v7 2/5] spi: spi-imx: add support for SPI_MOSI_IDLE_LOW mode bit

By default, the spi-imx controller pulls the mosi line high, whenever it
is idle. This behaviour can be inverted per CS by setting the
corresponding DATA_CTL bit in the config register of the controller.

Also, since the controller mode-bits have to be touched anyways, the
SPI_CPOL and SPI_CPHA are replaced by the combined SPI_MODE_X_MASK flag.

Signed-off-by: Boerge Struempfel <[email protected]>
---
drivers/spi/spi-imx.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/drivers/spi/spi-imx.c b/drivers/spi/spi-imx.c
index bd6ddb142b13..6f4d3cb81fdf 100644
--- a/drivers/spi/spi-imx.c
+++ b/drivers/spi/spi-imx.c
@@ -281,6 +281,7 @@ static bool spi_imx_can_dma(struct spi_controller *controller, struct spi_device
#define MX51_ECSPI_CONFIG_SCLKPOL(cs) (1 << ((cs & 3) + 4))
#define MX51_ECSPI_CONFIG_SBBCTRL(cs) (1 << ((cs & 3) + 8))
#define MX51_ECSPI_CONFIG_SSBPOL(cs) (1 << ((cs & 3) + 12))
+#define MX51_ECSPI_CONFIG_DATACTL(cs) (1 << ((cs & 3) + 16))
#define MX51_ECSPI_CONFIG_SCLKCTL(cs) (1 << ((cs & 3) + 20))

#define MX51_ECSPI_INT 0x10
@@ -573,6 +574,11 @@ static int mx51_ecspi_prepare_message(struct spi_imx_data *spi_imx,
cfg &= ~MX51_ECSPI_CONFIG_SCLKCTL(spi_get_chipselect(spi, 0));
}

+ if (spi->mode & SPI_MOSI_IDLE_LOW)
+ cfg |= MX51_ECSPI_CONFIG_DATACTL(spi_get_chipselect(spi, 0));
+ else
+ cfg &= ~MX51_ECSPI_CONFIG_DATACTL(spi_get_chipselect(spi, 0));
+
if (spi->mode & SPI_CS_HIGH)
cfg |= MX51_ECSPI_CONFIG_SSBPOL(spi_get_chipselect(spi, 0));
else
@@ -1743,7 +1749,8 @@ static int spi_imx_probe(struct platform_device *pdev)
controller->prepare_message = spi_imx_prepare_message;
controller->unprepare_message = spi_imx_unprepare_message;
controller->slave_abort = spi_imx_slave_abort;
- controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_NO_CS;
+ controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_NO_CS |
+ SPI_MOSI_IDLE_LOW;

if (is_imx35_cspi(spi_imx) || is_imx51_ecspi(spi_imx) ||
is_imx53_ecspi(spi_imx))
--
2.40.1


2023-05-30 14:40:45

by Boerge Struempfel

[permalink] [raw]
Subject: [PATCH v7 5/5] spi: spidev_test Add three missing spi mode bits

Added the three missing spi mode bits SPI_3WIRE_HIZ, SPI_RX_CPHA_FLIP,
and SPI_MOSI_IDLE_LOW.

Signed-off-by: Boerge Struempfel <[email protected]>
---
tools/spi/spidev_test.c | 19 +++++++++++++++++--
1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c
index 2d2cee339b39..9179942d7f15 100644
--- a/tools/spi/spidev_test.c
+++ b/tools/spi/spidev_test.c
@@ -172,7 +172,7 @@ static void transfer(int fd, uint8_t const *tx, uint8_t const *rx, size_t len)

static void print_usage(const char *prog)
{
- printf("Usage: %s [-2348CDHILNORSbdilopsv]\n", prog);
+ printf("Usage: %s [-2348CDFHILMNORSZbdilopsv]\n", prog);
puts("general device settings:\n"
" -D --device device to use (default /dev/spidev1.1)\n"
" -s --speed max speed (Hz)\n"
@@ -181,11 +181,13 @@ static void print_usage(const char *prog)
"spi mode:\n"
" -H --cpha clock phase\n"
" -O --cpol clock polarity\n"
+ " -F --rx-cpha-flip flip CPHA on Rx only xfer\n"
"number of wires for transmission:\n"
" -2 --dual dual transfer\n"
" -4 --quad quad transfer\n"
" -8 --octal octal transfer\n"
" -3 --3wire SI/SO signals shared\n"
+ " -Z --3wire-hiz high impedance turnaround\n"
"data:\n"
" -i --input input data from a file (e.g. \"test.bin\")\n"
" -o --output output data to a file (e.g. \"results.bin\")\n"
@@ -198,6 +200,7 @@ static void print_usage(const char *prog)
" -C --cs-high chip select active high\n"
" -N --no-cs no chip select\n"
" -R --ready slave pulls low to pause\n"
+ " -M --mosi-idle-low leave mosi line low when idle\n"
"misc:\n"
" -v --verbose Verbose (show tx buffer)\n");
exit(1);
@@ -213,10 +216,12 @@ static void parse_opts(int argc, char *argv[])
{ "loop", 0, 0, 'l' },
{ "cpha", 0, 0, 'H' },
{ "cpol", 0, 0, 'O' },
+ { "rx-cpha-flip", 0, 0, 'F' },
{ "dual", 0, 0, '2' },
{ "quad", 0, 0, '4' },
{ "octal", 0, 0, '8' },
{ "3wire", 0, 0, '3' },
+ { "3wire-hiz", 0, 0, 'Z' },
{ "input", 1, 0, 'i' },
{ "output", 1, 0, 'o' },
{ "size", 1, 0, 'S' },
@@ -226,12 +231,13 @@ static void parse_opts(int argc, char *argv[])
{ "cs-high", 0, 0, 'C' },
{ "no-cs", 0, 0, 'N' },
{ "ready", 0, 0, 'R' },
+ { "mosi-idle-low", 0, 0, 'M' },
{ "verbose", 0, 0, 'v' },
{ NULL, 0, 0, 0 },
};
int c;

- c = getopt_long(argc, argv, "D:s:d:b:i:o:lHOLC3NR248p:vS:I:",
+ c = getopt_long(argc, argv, "D:s:d:b:i:o:lHOLC3ZFMNR248p:vS:I:",
lopts, NULL);

if (c == -1)
@@ -274,6 +280,15 @@ static void parse_opts(int argc, char *argv[])
case '3':
mode |= SPI_3WIRE;
break;
+ case 'Z':
+ mode |= SPI_3WIRE_HIZ;
+ break;
+ case 'F':
+ mode |= SPI_RX_CPHA_FLIP;
+ break;
+ case 'M':
+ mode |= SPI_MOSI_IDLE_LOW;
+ break;
case 'N':
mode |= SPI_NO_CS;
break;
--
2.40.1


2023-05-30 14:42:21

by Boerge Struempfel

[permalink] [raw]
Subject: [PATCH v7 3/5] spi: spidev: add two new spi mode bits

Allow userspace to set SPI_MOSI_IDLE_LOW and the SPI_3WIRE_HIZ mode bit
using the SPI_IOC_WR_MODE32 ioctl.

Signed-off-by: Boerge Struempfel <[email protected]>
---
drivers/spi/spidev.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/spi/spidev.c b/drivers/spi/spidev.c
index 39d94c850839..c8b938e0f342 100644
--- a/drivers/spi/spidev.c
+++ b/drivers/spi/spidev.c
@@ -64,7 +64,8 @@ static_assert(N_SPI_MINORS > 0 && N_SPI_MINORS <= 256);
| SPI_NO_CS | SPI_READY | SPI_TX_DUAL \
| SPI_TX_QUAD | SPI_TX_OCTAL | SPI_RX_DUAL \
| SPI_RX_QUAD | SPI_RX_OCTAL \
- | SPI_RX_CPHA_FLIP)
+ | SPI_RX_CPHA_FLIP | SPI_3WIRE_HIZ \
+ | SPI_MOSI_IDLE_LOW)

struct spidev_data {
dev_t devt;
--
2.40.1


2023-05-30 17:55:26

by Mark Brown

[permalink] [raw]
Subject: Re: [PATCH v7 0/5] spi: add SPI_MOSI_IDLE_LOW mode bit

On Tue, 30 May 2023 16:16:36 +0200, Boerge Struempfel wrote:
> Some spi controller switch the mosi line to high, whenever they are
> idle. This may not be desired in all use cases. For example neopixel
> leds can get confused and flicker due to misinterpreting the idle state.
> Therefore, we introduce a new spi-mode bit, with which the idle behaviour
> can be overwritten on a per device basis.
>

Applied to

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git for-next

Thanks!

[1/5] spi: add SPI_MOSI_IDLE_LOW mode bit
commit: fe73245592fef75a7c41180a3fbb07c9a75f622e
[2/5] spi: spi-imx: add support for SPI_MOSI_IDLE_LOW mode bit
commit: 6a983ff5102ff0d859df05ca3f5cf2f6a17c0fad
[3/5] spi: spidev: add two new spi mode bits
commit: 5cc223ca4858ec40bd2b8522ebc51c0d4963e51f
[4/5] spi: spidev_test: Sorted the options into logical groups
commit: 113f36f2dce3a5a1aacbfa700c44080ec37ee3a0
[5/5] spi: spidev_test Add three missing spi mode bits
commit: b229a7f530ebea90c8e21b56872f3102e3d54461

All being well this means that it will be integrated into the linux-next
tree (usually sometime in the next 24 hours) and sent to Linus during
the next merge window (or sooner if it is a bug fix), however if
problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing
and review of the tree, please engage with people reporting problems and
send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they
should be sent as incremental updates against current git, existing
patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying
to this mail.

Thanks,
Mark