Skip to contentSkip to conversation

drm/mipi-dbi: Allow using same the D/C GPIO for multiple displays

PATCH Superseded Otto Pflüger 2 participants 6 messages Jul 20, 2023 – Jul 23, 2023

Subsystems (5)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover drm/mipi-dbi: Allow using same the D/C GPIO for multiple displays

When multiple displays are connected to the same SPI bus, the data/command switch is sometimes considered part of the bus and is shared among the displays.

This series adds the GPIO_FLAGS_BIT_NONEXCLUSIVE flag for this GPIO and SPI bus locking to the panel-mipi-dbi/drm_mipi_dbi drivers to support this hardware setup.

---
Changes in v2:
- fix uses of mipi_dbi_spi_transfer outside drm_mipi_dbi.c (errors reported by kernel test robot)
- remove the is_locked argument introduced in v1 which was always set to true

Otto Pflüger (2):
drm/mipi-dbi: Lock SPI bus before setting D/C GPIO drm/tiny: panel-mipi-dbi: Allow sharing the D/C GPIO

 drivers/gpu/drm/drm_mipi_dbi.c        | 17 +++++++++++++----
 drivers/gpu/drm/tiny/ili9225.c        |  7 ++++++-
 drivers/gpu/drm/tiny/ili9486.c        |  4 ++++
 drivers/gpu/drm/tiny/panel-mipi-dbi.c |  3 ++-
 4 files changed, 25 insertions(+), 6 deletions(-)

base-commit: c58c49dd89324b18a812762a2bfa5a0458e4f252

Signature · 1 line
2.39.1
#1 lore raw

PATCH v2 1/2 drm/mipi-dbi: Lock SPI bus before setting D/C GPIO

Multiple displays may be connected to the same bus and share a D/C GPIO, so the display driver needs exclusive access to the bus to ensure that it can control the D/C GPIO safely.

---
 drivers/gpu/drm/drm_mipi_dbi.c | 17 +++++++++++++----
 drivers/gpu/drm/tiny/ili9225.c |  7 ++++++-
 drivers/gpu/drm/tiny/ili9486.c |  4 ++++
 3 files changed, 23 insertions(+), 5 deletions(-)
drivers/gpu/drm/drm_mipi_dbi.c +13 -4
index c871d9f096b8..e90f0bf895b3 100644
@@ -1140,10 +1140,13 @@ static int mipi_dbi_typec3_command_read(struct mipi_dbi *dbi, u8 *cmd,
 		return -ENOMEM;
 
 	tr[1].rx_buf = buf;
+
+	spi_bus_lock(spi->controller);
 	gpiod_set_value_cansleep(dbi->dc, 0);
 
 	spi_message_init_with_transfers(&m, tr, ARRAY_SIZE(tr));
-	ret = spi_sync(spi, &m);
+	ret = spi_sync_locked(spi, &m);
+	spi_bus_unlock(spi->controller);
 	if (ret)
 		goto err_free;
 
@@ -1177,19 +1180,24 @@ static int mipi_dbi_typec3_command(struct mipi_dbi *dbi, u8 *cmd,
 
 	MIPI_DBI_DEBUG_COMMAND(*cmd, par, num);
 
+	spi_bus_lock(spi->controller);
 	gpiod_set_value_cansleep(dbi->dc, 0);
 	speed_hz = mipi_dbi_spi_cmd_max_speed(spi, 1);
 	ret = mipi_dbi_spi_transfer(spi, speed_hz, 8, cmd, 1);
+	spi_bus_unlock(spi->controller);
 	if (ret || !num)
 		return ret;
 
 	if (*cmd == MIPI_DCS_WRITE_MEMORY_START && !dbi->swap_bytes)
 		bpw = 16;
 
+	spi_bus_lock(spi->controller);
 	gpiod_set_value_cansleep(dbi->dc, 1);
 	speed_hz = mipi_dbi_spi_cmd_max_speed(spi, num);
+	ret = mipi_dbi_spi_transfer(spi, speed_hz, bpw, par, num);
+	spi_bus_unlock(spi->controller);
 
-	return mipi_dbi_spi_transfer(spi, speed_hz, bpw, par, num);
+	return ret;
 }
 
 /**
@@ -1271,7 +1279,8 @@ EXPORT_SYMBOL(mipi_dbi_spi_init);
  * @len: Buffer length
  *
  * This SPI transfer helper breaks up the transfer of @buf into chunks which
- * the SPI controller driver can handle.
+ * the SPI controller driver can handle. The SPI bus must be locked when
+ * calling this.
  *
  * Returns:
  * Zero on success, negative error code on failure.
@@ -1305,7 +1314,7 @@ int mipi_dbi_spi_transfer(struct spi_device *spi, u32 speed_hz,
 		buf += chunk;
 		len -= chunk;
 
-		ret = spi_sync(spi, &m);
+		ret = spi_sync_locked(spi, &m);
 		if (ret)
 			return ret;
 	}
drivers/gpu/drm/tiny/ili9225.c +6 -1
index 077c6ff5a2e1..4ceb68ffac4b 100644
@@ -316,19 +316,24 @@ static int ili9225_dbi_command(struct mipi_dbi *dbi, u8 *cmd, u8 *par,
 	u32 speed_hz;
 	int ret;
 
+	spi_bus_lock(spi->controller);
 	gpiod_set_value_cansleep(dbi->dc, 0);
 	speed_hz = mipi_dbi_spi_cmd_max_speed(spi, 1);
 	ret = mipi_dbi_spi_transfer(spi, speed_hz, 8, cmd, 1);
+	spi_bus_unlock(spi->controller);
 	if (ret || !num)
 		return ret;
 
 	if (*cmd == ILI9225_WRITE_DATA_TO_GRAM && !dbi->swap_bytes)
 		bpw = 16;
 
+	spi_bus_lock(spi->controller);
 	gpiod_set_value_cansleep(dbi->dc, 1);
 	speed_hz = mipi_dbi_spi_cmd_max_speed(spi, num);
+	ret = mipi_dbi_spi_transfer(spi, speed_hz, bpw, par, num);
+	spi_bus_unlock(spi->controller);
 
-	return mipi_dbi_spi_transfer(spi, speed_hz, bpw, par, num);
+	return ret;
 }
 
 static const struct drm_simple_display_pipe_funcs ili9225_pipe_funcs = {
drivers/gpu/drm/tiny/ili9486.c +4 -0
index 02265c898816..938bceed5999 100644
@@ -59,9 +59,11 @@ static int waveshare_command(struct mipi_dbi *mipi, u8 *cmd, u8 *par,
 	 * before being transferred as 8-bit on the big endian SPI bus.
 	 */
 	buf[0] = cpu_to_be16(*cmd);
+	spi_bus_lock(spi->controller);
 	gpiod_set_value_cansleep(mipi->dc, 0);
 	speed_hz = mipi_dbi_spi_cmd_max_speed(spi, 2);
 	ret = mipi_dbi_spi_transfer(spi, speed_hz, 8, buf, 2);
+	spi_bus_unlock(spi->controller);
 	if (ret || !num)
 		goto free;
 
@@ -79,9 +81,11 @@ static int waveshare_command(struct mipi_dbi *mipi, u8 *cmd, u8 *par,
 	if (*cmd == MIPI_DCS_WRITE_MEMORY_START && !mipi->swap_bytes)
 		bpw = 16;
 
+	spi_bus_lock(spi->controller);
 	gpiod_set_value_cansleep(mipi->dc, 1);
 	speed_hz = mipi_dbi_spi_cmd_max_speed(spi, num);
 	ret = mipi_dbi_spi_transfer(spi, speed_hz, bpw, data, num);
+	spi_bus_unlock(spi->controller);
  free:
 	kfree(buf);
 
Signature · 1 line
2.39.1
#3 lore raw

Re: [PATCH v2 1/2] drm/mipi-dbi: Lock SPI bus before setting D/C GPIO

In reply to Otto Pflüger
Quoted 9 lines · Otto Pflüger

Multiple displays may be connected to the same bus and share a D/C GPIO, so the display driver needs exclusive access to the bus to ensure that it can control the D/C GPIO safely.

---
 drivers/gpu/drm/drm_mipi_dbi.c | 17 +++++++++++++----
 drivers/gpu/drm/tiny/ili9225.c |  7 ++++++-
 drivers/gpu/drm/tiny/ili9486.c |  4 ++++

You forgot to CC the driver maintainers, please add them in the next version of the patchset. The get_maintainer script should be able to pick them out for you.

Noralf.

#4 lore raw

PATCH v2 2/2 drm/tiny: panel-mipi-dbi: Allow sharing the D/C GPIO

In reply to Otto Pflüger

Displays that are connected to the same SPI bus may share the D/C GPIO. Use GPIOD_FLAGS_BIT_NONEXCLUSIVE to allow access to the same GPIO for multiple panel-mipi-dbi instances. Exclusive access to the GPIO during transfers is ensured by the locking in drm_mipi_dbi.c.

---
 drivers/gpu/drm/tiny/panel-mipi-dbi.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/tiny/panel-mipi-dbi.c +2 -1
index eb9f13f18a02..e616e3890043 100644
@@ -307,7 +307,8 @@ static int panel_mipi_dbi_spi_probe(struct spi_device *spi)
 	if (IS_ERR(dbi->reset))
 		return dev_err_probe(dev, PTR_ERR(dbi->reset), "Failed to get GPIO 'reset'\n");
 
-	dc = devm_gpiod_get_optional(dev, "dc", GPIOD_OUT_LOW);
+	dc = devm_gpiod_get_optional(dev, "dc",
+				     GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_NONEXCLUSIVE);
 	if (IS_ERR(dc))
 		return dev_err_probe(dev, PTR_ERR(dc), "Failed to get GPIO 'dc'\n");
 
Signature · 1 line
2.39.1
#5 lore raw

Re: [PATCH v2 2/2] drm/tiny: panel-mipi-dbi: Allow sharing the D/C GPIO

Quoted 15 lines · Otto Pflüger

Displays that are connected to the same SPI bus may share the D/C GPIO. Use GPIOD_FLAGS_BIT_NONEXCLUSIVE to allow access to the same GPIO for multiple panel-mipi-dbi instances. Exclusive access to the GPIO during transfers is ensured by the locking in drm_mipi_dbi.c.

---
 drivers/gpu/drm/tiny/panel-mipi-dbi.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/tiny/panel-mipi-dbi.c +0 -0
index eb9f13f18a02..e616e3890043 100644

drivers/gpu/drm/tiny/panel-mipi-dbi.c static int panel_mipi_dbi_spi_probe(struct spi_device *spi) · line 310

Quoted 4 lines
drivers/gpu/drm/tiny/panel-mipi-dbi.c +0 -1
@@ -307,7 +307,8 @@ static int panel_mipi_dbi_spi_probe(struct spi_device *spi)
 	if (IS_ERR(dbi->reset))
 		return dev_err_probe(dev, PTR_ERR(dbi->reset), "Failed to get GPIO 'reset'\n");
 
-	dc = devm_gpiod_get_optional(dev, "dc", GPIOD_OUT_LOW);

Please add a comment here that sharing only works when on the same SPI bus.

drivers/gpu/drm/tiny/panel-mipi-dbi.c static int panel_mipi_dbi_spi_probe(struct spi_device *spi) · line 311

Quoted 2 lines
drivers/gpu/drm/tiny/panel-mipi-dbi.c +2 -0
+	dc = devm_gpiod_get_optional(dev, "dc",
+				     GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_NONEXCLUSIVE);

I'd prefer this to be on one line, the 80 column limit has been upped to a 100 now.

Noralf.

Quoted 3 lines
drivers/gpu/drm/tiny/panel-mipi-dbi.c +0 -0
 	if (IS_ERR(dc))
 		return dev_err_probe(dev, PTR_ERR(dc), "Failed to get GPIO 'dc'\n");