[PATCH 0/5] video: add support for MIPI DBI interface

Many simple RGB panels use a MIPI DBI interface over SPI, so this patch adds support for this interface using an API compatible with Linux.
The MIPI DBI interface works similar to SPI using 8-bit words in the format of: COMMAND DATA1 DATA2 ... However the interface has two variations on this.
The first variation is implemented by this patch series: Using 9-bit words, with the first bit signalling whether the word is a COMMAND or DATA.
The second variation is not implemented by this series: A GPIO is held at a value during an 8-bit word to indicate if it's a COMMAND or WORD. Adding support for this wouldn't be too hard, but I don't have any hardware to test it on.
Linux seems to not implement the specification correctly: It sets CS inactive between words instead of keeping it active for the entire transaction. I'm copying this behaviour to keep compatibility in porting panels across from the kernel.
Signed-off-by: John Watts contact@jookia.org --- John Watts (5): spi: Return old wordlen in spit_set_wordlen spi: Limit spi_set_wordlen to SPI_DEFAULT_WORDLEN spi: Implement spi_set_wordlen for driver model spi: softspi: Support setting wordlen video: add support for MIPI DBI interface
drivers/spi/soft_spi.c | 7 +++++ drivers/spi/spi-uclass.c | 29 +++++++++++++++++++ drivers/spi/spi.c | 6 ++-- drivers/video/Kconfig | 6 ++++ drivers/video/Makefile | 1 + drivers/video/mipi_dbi.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++ include/mipi_dbi.h | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ include/spi.h | 2 +- 8 files changed, 191 insertions(+), 3 deletions(-) --- base-commit: c0ea27bccfb7d2d37fd36806ac2a2f7389099420 change-id: 20240601-mipi_dbi-aeda98f2f9d5
Best regards,

Return the current wordlen to the caller so they can restore it back when done. This is required to avoid clobbering the bus state.
Signed-off-by: John Watts contact@jookia.org --- drivers/spi/spi.c | 4 +++- include/spi.h | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index 22910de0dd..3649c9c25b 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -10,6 +10,8 @@
int spi_set_wordlen(struct spi_slave *slave, unsigned int wordlen) { + int oldwordlen = slave->wordlen; + if (wordlen == 0 || wordlen > 32) { printf("spi: invalid wordlen %u\n", wordlen); return -1; @@ -17,7 +19,7 @@ int spi_set_wordlen(struct spi_slave *slave, unsigned int wordlen)
slave->wordlen = wordlen;
- return 0; + return oldwordlen; }
void *spi_do_alloc_slave(int offset, int size, unsigned int bus, diff --git a/include/spi.h b/include/spi.h index 7e38cc2a2a..d5acf4f9e9 100644 --- a/include/spi.h +++ b/include/spi.h @@ -259,7 +259,7 @@ void spi_release_bus(struct spi_slave *slave); * @slave: The SPI slave * @wordlen: The number of bits in a word * - * Returns: 0 on success, -1 on failure. + * Returns: The old word length on success, -1 on failure. */ int spi_set_wordlen(struct spi_slave *slave, unsigned int wordlen);

We have no way to tell if we can actually set the wordlen in non-DM code, so limit it to the default wordlen already in use.
Signed-off-by: John Watts contact@jookia.org --- drivers/spi/spi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index 3649c9c25b..7be9bf9b17 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -12,7 +12,7 @@ int spi_set_wordlen(struct spi_slave *slave, unsigned int wordlen) { int oldwordlen = slave->wordlen;
- if (wordlen == 0 || wordlen > 32) { + if (wordlen != SPI_DEFAULT_WORDLEN) { printf("spi: invalid wordlen %u\n", wordlen); return -1; }

Implement spi_set_wordlen for driver model devices.
Not all drivers support this interface, so we will assume the default wordlen is safe and allow setting that unconditionally.
Signed-off-by: John Watts contact@jookia.org --- drivers/spi/spi-uclass.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+)
diff --git a/drivers/spi/spi-uclass.c b/drivers/spi/spi-uclass.c index f4795e6867..c2baf9a262 100644 --- a/drivers/spi/spi-uclass.c +++ b/drivers/spi/spi-uclass.c @@ -130,6 +130,35 @@ void spi_release_bus(struct spi_slave *slave) dm_spi_release_bus(slave->dev); }
+static int spi_try_set_wordlen(struct spi_slave *slave, unsigned int wordlen) +{ + struct dm_spi_ops *ops; + + ops = spi_get_ops(slave->dev->parent); + if (ops->set_wordlen) + return ops->set_wordlen(slave->dev->parent, wordlen); + else if (wordlen == SPI_DEFAULT_WORDLEN) + return 0; + else + return -EINVAL; +} + +int spi_set_wordlen(struct spi_slave *slave, unsigned int wordlen) +{ + int oldwordlen = slave->wordlen; + int ret; + + ret = spi_try_set_wordlen(slave, wordlen); + if (ret < 0) { + dev_err(slave->dev, "Cannot set wordlen (err=%d)\n", ret); + return ret; + } + + slave->wordlen = wordlen; + + return oldwordlen; +} + int spi_set_speed(struct spi_slave *slave, uint hz) { struct dm_spi_ops *ops;

The bit-banging soft SPI driver supports any word length, so just blindly return support for all of them.
Signed-off-by: John Watts contact@jookia.org --- drivers/spi/soft_spi.c | 7 +++++++ 1 file changed, 7 insertions(+)
diff --git a/drivers/spi/soft_spi.c b/drivers/spi/soft_spi.c index 0fa14339bd..4f2161f0bf 100644 --- a/drivers/spi/soft_spi.c +++ b/drivers/spi/soft_spi.c @@ -204,6 +204,12 @@ static int soft_spi_xfer(struct udevice *dev, unsigned int bitlen, return 0; }
+static int soft_spi_set_wordlen(struct udevice *dev, unsigned int wordlen) +{ + /* Support any wordlen as this driver clock out bits individually. */ + return 0; +} + static int soft_spi_set_speed(struct udevice *dev, unsigned int speed) { /* Ignore any speed settings. Speed is implemented via "spi-delay-us" */ @@ -223,6 +229,7 @@ static const struct dm_spi_ops soft_spi_ops = { .claim_bus = soft_spi_claim_bus, .release_bus = soft_spi_release_bus, .xfer = soft_spi_xfer, + .set_wordlen = soft_spi_set_wordlen, .set_speed = soft_spi_set_speed, .set_mode = soft_spi_set_mode, };

This interface supports sending MIPI commands over an SPI bus. This driver only implements the Type C1 protocol for now.
Signed-off-by: John Watts contact@jookia.org --- drivers/video/Kconfig | 6 ++++ drivers/video/Makefile | 1 + drivers/video/mipi_dbi.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++ include/mipi_dbi.h | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 150 insertions(+)
diff --git a/drivers/video/Kconfig b/drivers/video/Kconfig index 7808ae7919..e9d069d440 100644 --- a/drivers/video/Kconfig +++ b/drivers/video/Kconfig @@ -157,6 +157,12 @@ config VIDEO_MIPI_DSI The MIPI Display Serial Interface (MIPI DSI) defines a high-speed serial interface between a host processor and a display module.
+config VIDEO_MIPI_DBI + bool "Support MIPI DBI interface" + help + Support MIPI DBI interface for driving a MIPI compatible device + over a SPI interface. + config CONSOLE_NORMAL bool "Support a simple text console" default y diff --git a/drivers/video/Makefile b/drivers/video/Makefile index f3f70cd04a..ad77c60973 100644 --- a/drivers/video/Makefile +++ b/drivers/video/Makefile @@ -69,6 +69,7 @@ obj-$(CONFIG_VIDEO_LCD_TDO_TL070WSH30) += tdo-tl070wsh30.o obj-$(CONFIG_VIDEO_MCDE_SIMPLE) += mcde_simple.o obj-${CONFIG_VIDEO_MESON} += meson/ obj-${CONFIG_VIDEO_MIPI_DSI} += mipi_dsi.o +obj-${CONFIG_VIDEO_MIPI_DBI} += mipi_dbi.o obj-$(CONFIG_VIDEO_MVEBU) += mvebu_lcd.o obj-$(CONFIG_VIDEO_MXS) += mxsfb.o videomodes.o obj-$(CONFIG_VIDEO_NX) += nexell_display.o videomodes.o nexell/ diff --git a/drivers/video/mipi_dbi.c b/drivers/video/mipi_dbi.c new file mode 100644 index 0000000000..d7457bb6e2 --- /dev/null +++ b/drivers/video/mipi_dbi.c @@ -0,0 +1,70 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * MIPI DBI Bus support + * + * Copyright 2024 John Watts contact@jookia.org + */ + +#include <mipi_dbi.h> + +int mipi_dbi_spi_init(struct spi_slave *slave, struct mipi_dbi *dbi, + struct gpio_desc *dc) +{ + /* D/C GPIO isn't supported yet */ + if (dc) + return -1; + + dbi->spi = slave; + + return 0; +} + +int mipi_dbi_xfer(struct mipi_dbi *dbi, u8 data, int pos, int len) +{ + struct spi_slave *spi = dbi->spi; + bool is_data = (pos != 0); + int flags = 0; + u8 buf[2]; + + /* Mimic Linux's behaviour of pulling CS active each word */ + flags |= SPI_XFER_ONCE; + + buf[0] = (is_data ? 0x80 : 0x00) | (data >> 1); + buf[1] = ((data & 0x1) << 7); + + return spi_xfer(spi, 9, &buf, NULL, flags); +} + +int mipi_dbi_command_buf(struct mipi_dbi *dbi, u8 cmd, const u8 *data, size_t len) +{ + struct spi_slave *spi = dbi->spi; + int wordlen; + int retval = -1; + + if (spi_claim_bus(spi)) + return -1; + + wordlen = spi_set_wordlen(spi, 9); + if (wordlen == -1) + goto done; + + if (mipi_dbi_xfer(dbi, cmd, 0, len) != 0) + goto done; + + for (int i = 1; i <= len; ++i) { + u8 dat = data[i - 1]; + + if (mipi_dbi_xfer(dbi, dat, i, len) != 0) + goto done; + } + + retval = 0; + +done: + if (wordlen != -1) + spi_set_wordlen(spi, wordlen); + + spi_release_bus(spi); + + return retval; +} diff --git a/include/mipi_dbi.h b/include/mipi_dbi.h new file mode 100644 index 0000000000..1c0c21ba81 --- /dev/null +++ b/include/mipi_dbi.h @@ -0,0 +1,73 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * MIPI DBI Bus support + * + * Copyright 2024 John Watts contact@jookia.org + */ +#ifndef MIPI_DBI_H +#define MIPI_DBI_H + +#include <asm/gpio.h> +#include <mipi_display.h> +#include <spi.h> + +/** + * struct mipi_dbi - MIPI DBI bus info + * + * This contains information about a MIPI DBI bus. + * Use mipi_dbi_spi_init to create and initialize this structure. + * + * @spi: SPI slave this bus operates on. + */ +struct mipi_dbi { + struct spi_slave *spi; +}; + +/** + * mipi_dbi_spi_init - Creates a new MIPI DBI bus + * + * Creates and sets up a 'struct mipi_dbi' using the provided SPI slave + * and optional D/C GPIO. + * + * @slave: SPI slave the bus is on + * @dbi: Destination mipi_dbi structure to initialize + * @dc: D/C GPIO (NULL if unused) + * + * Returns: 0 on success, -1 on failure. + */ +int mipi_dbi_spi_init(struct spi_slave *slave, struct mipi_dbi *dbi, + struct gpio_desc *dc); + +/** + * mipi_dbi_command_buf - Sends a command and data over the bus + * + * Sends a command and any optional data over a bus. + * + * @dbi: MIPI DBI bus to use + * @cmd: MIPI DBI command + * @data: Command data (NULL if len is 0) + * @len: Length of data in bytes + * + * Returns: 0 on success, -1 on failure. + */ +int mipi_dbi_command_buf(struct mipi_dbi *dbi, u8 cmd, const u8 *data, size_t len); + +/** + * mipi_dbi_command - Sends a command and data sequence over the bus + * + * Sends a command and any optional data over a bus. + * The data is a variadic sequence. + * + * @dbi: MIPI DBI bus to use + * @cmd: MIPI DBI command + * @seq: Command data bytes + * + * Returns: 0 on success, -1 on failure. + */ +#define mipi_dbi_command(dbi, cmd, seq...) \ +({ \ + const u8 data[] = { seq }; \ + mipi_dbi_command_buf(dbi, cmd, data, ARRAY_SIZE(data)); \ +}) + +#endif /* MIPI_DBI_H */
participants (1)
-
John Watts