* [PATCH v1 2/2] drm/sitronix: Add driver for Sitronix ST75256 LCD panels
2026-12-06 13:13 [PATCH v1 0/2] Add support for Sitronix ST75256 LCD panels Cyril Hrubis
2026-12-06 13:13 ` [PATCH v1 1/2] dt-bindings: display: sitronix,st75256: Add DT schema Cyril Hrubis
@ 2026-12-06 13:13 ` Cyril Hrubis
2026-12-06 13:35 ` sashiko-bot
1 sibling, 1 reply; 5+ messages in thread
From: Cyril Hrubis @ 2026-12-06 13:13 UTC (permalink / raw)
To: dri-devel, devicetree
Cc: lars.rask, Cyril Hrubis, David Airlie, Simona Vetter,
Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, Rob Herring,
Krzysztof Kozlowski, Conor Dooley, Javier Martinez Canillas,
Marcus Folkesson, Iker Pedrosa, linux-kernel
From: Cyril Hrubis <metan@ucw.cz>
Add a DRM driver for Sitronix ST75256 based monochrome and 4-level
grayscale LCD panels connected over 4-wire SPI, such as the JLX256128
and JLX256160 modules.
The controller is programmed to scan the display RAM page direction
first, which makes each panel column a row of pixels in memory, so the
panel is exposed in portrait orientation. In this layout the R1 and R2
formats match the display RAM layout of the monochrome and grayscale
mode and are transferred without conversion. XRGB8888 is supported as
well and converted to 4-level gray. The controller display mode is
switched based on the framebuffer format.
Only the damaged area, rounded to whole bytes, is transferred on
updates, which is about 0.4 ms for small updates compared to about 3.5
ms (R1) and 7 ms (R2) for a full 160x256 frame at the 12.5 MHz maximal
SPI clock.
The initialization sequence follows the datasheet reference sequence,
the factory Vop offset is loaded from the OTP. The Vop (contrast) and
optionally the gray levels are read from the device tree since they are
panel specific.
Tested on Raspberry Pi Zero W with JLX256128 and JLX256160 panels.
Signed-off-by: Cyril Hrubis <metan@ucw.cz>
---
MAINTAINERS | 7 +
drivers/gpu/drm/sitronix/Kconfig | 15 +
drivers/gpu/drm/sitronix/Makefile | 1 +
drivers/gpu/drm/sitronix/st75256.c | 879 +++++++++++++++++++++++++++++
4 files changed, 902 insertions(+)
create mode 100644 drivers/gpu/drm/sitronix/st75256.c
diff --git a/MAINTAINERS b/MAINTAINERS
index 84cb98ef4c49..f99f43beb121 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -8726,6 +8726,13 @@ T: git https://gitlab.freedesktop.org/drm/misc/kernel.git
F: Documentation/devicetree/bindings/display/sitronix,st7586.yaml
F: drivers/gpu/drm/sitronix/st7586.c
+DRM DRIVER FOR SITRONIX ST75256 PANELS
+M: Cyril Hrubis <metan@ucw.cz>
+S: Maintained
+T: git https://gitlab.freedesktop.org/drm/misc/kernel.git
+F: Documentation/devicetree/bindings/display/sitronix,st75256.yaml
+F: drivers/gpu/drm/sitronix/st75256.c
+
DRM DRIVER FOR SITRONIX ST7571 PANELS
M: Marcus Folkesson <marcus.folkesson@gmail.com>
S: Maintained
diff --git a/drivers/gpu/drm/sitronix/Kconfig b/drivers/gpu/drm/sitronix/Kconfig
index 41a428ef8295..907ee32cc9b2 100644
--- a/drivers/gpu/drm/sitronix/Kconfig
+++ b/drivers/gpu/drm/sitronix/Kconfig
@@ -39,6 +39,21 @@ config DRM_ST7571_SPI
if M is selected the module will be called st7571-spi.
+config DRM_ST75256
+ tristate "DRM support for Sitronix ST75256 display panels"
+ depends on DRM && SPI && MMU
+ select BACKLIGHT_CLASS_DEVICE
+ select DRM_CLIENT_SELECTION
+ select DRM_GEM_SHMEM_HELPER
+ select DRM_KMS_HELPER
+ select VIDEOMODE_HELPERS
+ help
+ DRM driver for Sitronix ST75256 based monochrome and 4-level
+ grayscale LCD panels connected over 4-wire SPI, e.g. JLX256128
+ and JLX256160.
+
+ If M is selected the module will be called st75256.
+
config DRM_ST7586
tristate "DRM support for Sitronix ST7586 display panels"
depends on DRM && SPI
diff --git a/drivers/gpu/drm/sitronix/Makefile b/drivers/gpu/drm/sitronix/Makefile
index d03beff37628..7eba92a5b1fe 100644
--- a/drivers/gpu/drm/sitronix/Makefile
+++ b/drivers/gpu/drm/sitronix/Makefile
@@ -1,6 +1,7 @@
obj-$(CONFIG_DRM_ST7571) += st7571.o
obj-$(CONFIG_DRM_ST7571_I2C) += st7571-i2c.o
obj-$(CONFIG_DRM_ST7571_SPI) += st7571-spi.o
+obj-$(CONFIG_DRM_ST75256) += st75256.o
obj-$(CONFIG_DRM_ST7586) += st7586.o
obj-$(CONFIG_DRM_ST7735R) += st7735r.o
obj-$(CONFIG_DRM_ST7920) += st7920.o
diff --git a/drivers/gpu/drm/sitronix/st75256.c b/drivers/gpu/drm/sitronix/st75256.c
new file mode 100644
index 000000000000..4a931208cec5
--- /dev/null
+++ b/drivers/gpu/drm/sitronix/st75256.c
@@ -0,0 +1,879 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * DRM driver for Sitronix ST75256 based monochrome and 4-level grayscale
+ * LCD panels connected over 4-wire SPI.
+ *
+ * The controller is programmed to scan the display RAM page direction first,
+ * which makes each column of the panel a row of pixels in memory. The panel
+ * is therefore exposed in portrait orientation, i.e. the 256 pixel long side
+ * of the panel is vertical. In this layout the DRM_FORMAT_R1 and
+ * DRM_FORMAT_R2 formats (first pixel in the most significant bits) match the
+ * display RAM layout in monochrome and grayscale mode respectively and are
+ * sent to the controller as they are.
+ *
+ * Copyright (C) 2026 Cyril Hrubis <metan@ucw.cz>
+ */
+
+#include <linux/backlight.h>
+#include <linux/delay.h>
+#include <linux/gpio/consumer.h>
+#include <linux/iosys-map.h>
+#include <linux/module.h>
+#include <linux/property.h>
+#include <linux/spi/spi.h>
+
+#include <video/display_timing.h>
+#include <video/of_display_timing.h>
+
+#include <drm/clients/drm_client_setup.h>
+#include <drm/drm_atomic.h>
+#include <drm/drm_atomic_helper.h>
+#include <drm/drm_crtc_helper.h>
+#include <drm/drm_damage_helper.h>
+#include <drm/drm_drv.h>
+#include <drm/drm_fbdev_shmem.h>
+#include <drm/drm_format_helper.h>
+#include <drm/drm_fourcc.h>
+#include <drm/drm_framebuffer.h>
+#include <drm/drm_gem_atomic_helper.h>
+#include <drm/drm_gem_framebuffer_helper.h>
+#include <drm/drm_gem_shmem_helper.h>
+#include <drm/drm_modeset_helper_vtables.h>
+#include <drm/drm_print.h>
+#include <drm/drm_probe_helper.h>
+#include <drm/drm_rect.h>
+
+#define DRIVER_NAME "st75256"
+#define DRIVER_DESC "DRM driver for Sitronix ST75256 LCD panels"
+#define DRIVER_MAJOR 1
+#define DRIVER_MINOR 0
+
+/* Command set selection */
+#define ST75256_EXT1 0x30
+#define ST75256_EXT2 0x31
+
+/* Extension 1 commands */
+#define ST75256_SET_COL_ADDR 0x15
+#define ST75256_PWR_CTRL 0x20
+#define ST75256_WRITE_DATA 0x5c
+#define ST75256_SET_PAGE_ADDR 0x75
+#define ST75256_SET_VOP 0x81
+#define ST75256_SLEEP_OUT 0x94
+#define ST75256_SLEEP_IN 0x95
+#define ST75256_DISP_INVERSE 0xa7
+#define ST75256_DISP_OFF 0xae
+#define ST75256_DISP_ON 0xaf
+#define ST75256_SET_SCAN_DIR 0xbc
+#define ST75256_DISP_CTRL 0xca
+#define ST75256_DISP_MODE 0xf0
+
+/* Extension 2 commands */
+#define ST75256_SET_GRAY_LEVEL 0x20
+#define ST75256_ANALOG_CIRCUIT 0x32
+#define ST75256_BOOSTER_LEVEL 0x51
+#define ST75256_OTP_AUTO_READ_CTRL 0xd7
+#define ST75256_OTP_WR_RD_CTRL 0xe0
+#define ST75256_OTP_CTRL_OUT 0xe1
+#define ST75256_OTP_READ 0xe3
+#define ST75256_FRAME_RATE 0xf0
+
+/* ST75256_PWR_CTRL bits */
+#define ST75256_PWR_VR BIT(0)
+#define ST75256_PWR_VF BIT(1)
+#define ST75256_PWR_VB BIT(3)
+
+/* ST75256_SET_SCAN_DIR bits */
+#define ST75256_SCAN_MX BIT(1)
+#define ST75256_SCAN_PAGE_FIRST BIT(2)
+
+/* ST75256_ANALOG_CIRCUIT parameters */
+#define ST75256_BOOSTER_EFF_LEVEL1 0x01
+#define ST75256_BIAS_1_12 0x02
+
+/* ST75256_BOOSTER_LEVEL values */
+#define ST75256_BOOSTER_X10 0xfb
+
+/* ST75256_DISP_MODE values */
+#define ST75256_MODE_MONO 0x10
+#define ST75256_MODE_GRAY 0x11
+
+/* ST75256_SET_VOP range, V0 = 3.6V + Vop * 0.04V, i.e. 7.0V to 18.0V */
+#define ST75256_VOP_MIN 0x55
+#define ST75256_VOP_MAX 0x168
+
+/* ST75256_SET_GRAY_LEVEL light and dark gray level maximum */
+#define ST75256_GRAY_LEVEL_MAX 0x1f
+
+/*
+ * Default gray levels, calibrated on JLX256128 and JLX256160 panels. They
+ * differ from the controller defaults since the display is inverted
+ * (ST75256_DISP_INVERSE).
+ */
+#define ST75256_GRAY_LIGHT_DEFAULT 0x14
+#define ST75256_GRAY_DARK_DEFAULT 0x0f
+
+/*
+ * Maximal panel size in the portrait orientation, 160 of the 162 COM lines
+ * (whole bytes in monochrome mode) and 256 SEG lines.
+ */
+#define ST75256_MAX_WIDTH 160
+#define ST75256_MAX_HEIGHT 256
+
+#define ST75256_CMD_BUF_LEN 16
+
+struct st75256_panel {
+ /* Pixels along the page (COM) direction, multiple of 8 */
+ unsigned int width;
+ /* Pixels along the column (SEG) direction */
+ unsigned int height;
+ unsigned int width_mm;
+ unsigned int height_mm;
+ /* LCD operating voltage (contrast) */
+ u16 vop;
+ /* Gray levels for 4-level grayscale mode */
+ u8 gray_light;
+ u8 gray_dark;
+};
+
+struct st75256_device {
+ struct drm_device drm;
+ struct drm_plane plane;
+ struct drm_crtc crtc;
+ struct drm_encoder encoder;
+ struct drm_connector connector;
+ struct drm_display_mode mode;
+
+ struct spi_device *spi;
+ struct gpio_desc *reset;
+ struct gpio_desc *dc;
+ struct backlight_device *backlight;
+ /* Panel parameters from the device tree */
+ struct st75256_panel panel;
+
+ /* Currently programmed ST75256_MODE_*, 0 when unknown */
+ u8 hw_mode;
+
+ /* DMA safe buffers */
+ u8 *cmd_buf;
+ u8 *tx_buf;
+
+ /* One line of 8-bit gray pixels for the XRGB8888 conversion */
+ u8 *line_buf;
+};
+
+static inline struct st75256_device *drm_to_st75256(struct drm_device *drm)
+{
+ return container_of(drm, struct st75256_device, drm);
+}
+
+static int st75256_spi_write(struct st75256_device *st75256, bool data,
+ const u8 *buf, size_t len)
+{
+ size_t max_len = spi_max_transfer_size(st75256->spi);
+ int ret;
+
+ gpiod_set_value_cansleep(st75256->dc, data);
+
+ while (len) {
+ size_t chunk = min(len, max_len);
+
+ ret = spi_write(st75256->spi, buf, chunk);
+ if (ret)
+ return ret;
+
+ buf += chunk;
+ len -= chunk;
+ }
+
+ return 0;
+}
+
+static int st75256_cmd_buf(struct st75256_device *st75256, u8 cmd,
+ const u8 *params, size_t num)
+{
+ int ret;
+
+ if (WARN_ON(num > ST75256_CMD_BUF_LEN))
+ return -EINVAL;
+
+ st75256->cmd_buf[0] = cmd;
+ ret = st75256_spi_write(st75256, false, st75256->cmd_buf, 1);
+ if (ret || !num)
+ return ret;
+
+ memcpy(st75256->cmd_buf, params, num);
+
+ return st75256_spi_write(st75256, true, st75256->cmd_buf, num);
+}
+
+/*
+ * Sends a command followed by optional parameters. Does nothing if a previous
+ * command failed, so that a sequence can be checked for errors once at the end.
+ */
+#define st75256_cmd(st75256, err, cmd, seq...) \
+ do { \
+ const u8 d[] = { seq }; \
+ int *e = (err); \
+ \
+ if (!*e) \
+ *e = st75256_cmd_buf(st75256, cmd, d, \
+ ARRAY_SIZE(d)); \
+ } while (0)
+
+/*
+ * Sets the display RAM window and writes the data. The x coordinates are in
+ * bytes (pages), y coordinates in pixels (columns), the end is exclusive.
+ */
+static int st75256_write_window(struct st75256_device *st75256,
+ unsigned int x1, unsigned int x2,
+ unsigned int y1, unsigned int y2,
+ const u8 *buf, size_t len)
+{
+ int err = 0;
+
+ st75256_cmd(st75256, &err, ST75256_SET_COL_ADDR, y1, y2 - 1);
+ st75256_cmd(st75256, &err, ST75256_SET_PAGE_ADDR, x1, x2 - 1);
+ st75256_cmd(st75256, &err, ST75256_WRITE_DATA);
+ if (err)
+ return err;
+
+ return st75256_spi_write(st75256, true, buf, len);
+}
+
+static int st75256_clear(struct st75256_device *st75256)
+{
+ const struct st75256_panel *panel = &st75256->panel;
+ unsigned int ppb = st75256->hw_mode == ST75256_MODE_MONO ? 8 : 4;
+ size_t len = panel->width / ppb * panel->height;
+
+ memset(st75256->tx_buf, 0, len);
+
+ return st75256_write_window(st75256, 0, panel->width / ppb,
+ 0, panel->height, st75256->tx_buf, len);
+}
+
+static int st75256_hw_init(struct st75256_device *st75256)
+{
+ const struct st75256_panel *panel = &st75256->panel;
+ int err = 0;
+
+ gpiod_set_value_cansleep(st75256->reset, 1);
+ usleep_range(1000, 2000);
+ gpiod_set_value_cansleep(st75256->reset, 0);
+ msleep(120);
+
+ st75256_cmd(st75256, &err, ST75256_EXT1);
+ st75256_cmd(st75256, &err, ST75256_SLEEP_OUT);
+ if (err)
+ return err;
+ msleep(50);
+ st75256_cmd(st75256, &err, ST75256_DISP_OFF);
+
+ /* Load the factory calibration from OTP */
+ st75256_cmd(st75256, &err, ST75256_EXT2);
+ st75256_cmd(st75256, &err, ST75256_OTP_AUTO_READ_CTRL, 0x9f);
+ st75256_cmd(st75256, &err, ST75256_OTP_WR_RD_CTRL, 0x00);
+ if (err)
+ return err;
+ usleep_range(10000, 11000);
+ st75256_cmd(st75256, &err, ST75256_OTP_READ);
+ if (err)
+ return err;
+ msleep(20);
+ st75256_cmd(st75256, &err, ST75256_OTP_CTRL_OUT);
+
+ /* Frame rate 55.5 Hz */
+ st75256_cmd(st75256, &err, ST75256_FRAME_RATE, 0x09, 0x09, 0x09, 0x09);
+
+ st75256_cmd(st75256, &err, ST75256_EXT1);
+ st75256_cmd(st75256, &err, ST75256_SET_SCAN_DIR,
+ ST75256_SCAN_PAGE_FIRST | ST75256_SCAN_MX);
+ /* Inverse display, 0 is black */
+ st75256_cmd(st75256, &err, ST75256_DISP_INVERSE);
+ st75256_cmd(st75256, &err, ST75256_PWR_CTRL,
+ ST75256_PWR_VB | ST75256_PWR_VF | ST75256_PWR_VR);
+ if (err)
+ return err;
+ usleep_range(1000, 2000);
+
+ st75256_cmd(st75256, &err, ST75256_SET_VOP,
+ panel->vop & 0x3f, (panel->vop >> 6) & 0x07);
+
+ st75256_cmd(st75256, &err, ST75256_EXT2);
+ st75256_cmd(st75256, &err, ST75256_SET_GRAY_LEVEL,
+ 0x00, 0x00, 0x00,
+ panel->gray_light, panel->gray_light, panel->gray_light,
+ 0x00, 0x00,
+ panel->gray_dark,
+ 0x00, 0x00,
+ panel->gray_dark, panel->gray_dark, panel->gray_dark,
+ 0x00, 0x00);
+ st75256_cmd(st75256, &err, ST75256_ANALOG_CIRCUIT, 0x00,
+ ST75256_BOOSTER_EFF_LEVEL1, ST75256_BIAS_1_12);
+ st75256_cmd(st75256, &err, ST75256_BOOSTER_LEVEL, ST75256_BOOSTER_X10);
+ st75256_cmd(st75256, &err, ST75256_EXT1);
+
+ st75256_cmd(st75256, &err, ST75256_DISP_MODE, ST75256_MODE_GRAY);
+ /* Clock divider, duty, line inversion */
+ st75256_cmd(st75256, &err, ST75256_DISP_CTRL, 0x00, panel->width - 1, 0x01);
+ if (err)
+ return err;
+
+ st75256->hw_mode = ST75256_MODE_GRAY;
+
+ err = st75256_clear(st75256);
+
+ st75256_cmd(st75256, &err, ST75256_DISP_ON);
+
+ return err;
+}
+
+static void st75256_hw_fini(struct st75256_device *st75256)
+{
+ int err = 0;
+
+ st75256_cmd(st75256, &err, ST75256_EXT1);
+ st75256_cmd(st75256, &err, ST75256_DISP_OFF);
+ st75256_cmd(st75256, &err, ST75256_SLEEP_IN);
+
+ gpiod_set_value_cansleep(st75256->reset, 1);
+ st75256->hw_mode = 0;
+}
+
+static int st75256_set_mode(struct st75256_device *st75256, u8 mode)
+{
+ int err = 0;
+
+ st75256_cmd(st75256, &err, ST75256_DISP_MODE, mode);
+ if (!err)
+ st75256->hw_mode = mode;
+
+ return err;
+}
+
+/* Packs 8-bit gray pixels into 2-bit pixels, first pixel in the MSBs */
+static void st75256_pack_gray2(u8 *dst, const u8 *gray, unsigned int pixels)
+{
+ unsigned int i;
+
+ for (i = 0; i < pixels; i += 4) {
+ *dst++ = (gray[i] & 0xc0) |
+ ((gray[i + 1] & 0xc0) >> 2) |
+ ((gray[i + 2] & 0xc0) >> 4) |
+ (gray[i + 3] >> 6);
+ }
+}
+
+static int st75256_fb_blit(struct st75256_device *st75256,
+ struct drm_framebuffer *fb,
+ const struct iosys_map *vmap,
+ const struct drm_plane_state *state,
+ const struct drm_rect *damage,
+ struct drm_format_conv_state *fmtcnv_state)
+{
+ unsigned int ppb = st75256->hw_mode == ST75256_MODE_MONO ? 8 : 4;
+ int src_x = state->src.x1 >> 16;
+ int src_y = state->src.y1 >> 16;
+ struct drm_rect dst = *damage;
+ unsigned int row_len;
+ u8 *tx = st75256->tx_buf;
+ int y;
+
+ /* Framebuffer to display coordinates, aligned to whole bytes */
+ drm_rect_translate(&dst, -src_x, -src_y);
+ dst.x1 = round_down(dst.x1, ppb);
+ dst.x2 = min_t(int, round_up(dst.x2, ppb), st75256->panel.width);
+ row_len = drm_rect_width(&dst) / ppb;
+
+ if (fb->format->format == DRM_FORMAT_XRGB8888) {
+ struct iosys_map line = IOSYS_MAP_INIT_VADDR(st75256->line_buf);
+
+ for (y = dst.y1; y < dst.y2; y++) {
+ struct drm_rect clip = DRM_RECT_INIT(dst.x1 + src_x, y + src_y,
+ drm_rect_width(&dst), 1);
+
+ drm_fb_xrgb8888_to_gray8(&line, NULL, vmap, fb, &clip,
+ fmtcnv_state);
+ st75256_pack_gray2(tx, st75256->line_buf,
+ drm_rect_width(&dst));
+ tx += row_len;
+ }
+ } else {
+ const u8 *src = (const u8 *)vmap->vaddr + fb->offsets[0] +
+ (dst.x1 + src_x) / ppb;
+
+ for (y = dst.y1; y < dst.y2; y++) {
+ memcpy(tx, src + (y + src_y) * fb->pitches[0], row_len);
+ tx += row_len;
+ }
+ }
+
+ return st75256_write_window(st75256, dst.x1 / ppb, dst.x2 / ppb,
+ dst.y1, dst.y2, st75256->tx_buf,
+ tx - st75256->tx_buf);
+}
+
+static const u32 st75256_formats[] = {
+ DRM_FORMAT_R1,
+ DRM_FORMAT_R2,
+ DRM_FORMAT_XRGB8888,
+};
+
+static int st75256_plane_atomic_check(struct drm_plane *plane,
+ struct drm_atomic_commit *state)
+{
+ struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state, plane);
+ struct drm_shadow_plane_state *shadow_state = to_drm_shadow_plane_state(new_state);
+ struct drm_framebuffer *fb = new_state->fb;
+ struct drm_crtc_state *crtc_state = NULL;
+ int ret;
+
+ if (new_state->crtc)
+ crtc_state = drm_atomic_get_new_crtc_state(state, new_state->crtc);
+
+ ret = drm_atomic_helper_check_plane_state(new_state, crtc_state,
+ DRM_PLANE_NO_SCALING,
+ DRM_PLANE_NO_SCALING,
+ false, false);
+ if (ret || !new_state->visible)
+ return ret;
+
+ if (fb->format->format == DRM_FORMAT_XRGB8888) {
+ /* Line buffer used by drm_fb_xrgb8888_to_gray8() */
+ if (!drm_format_conv_state_reserve(&shadow_state->fmtcnv_state,
+ drm_rect_width(&new_state->dst) *
+ fb->format->cpp[0], GFP_KERNEL))
+ return -ENOMEM;
+ } else {
+ /* Packed pixels are copied as whole bytes */
+ unsigned int ppb = 8 / fb->format->depth;
+
+ if ((new_state->src.x1 >> 16) % ppb)
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+static void st75256_plane_atomic_update(struct drm_plane *plane,
+ struct drm_atomic_commit *state)
+{
+ struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state, plane);
+ struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state, plane);
+ struct drm_shadow_plane_state *shadow_state = to_drm_shadow_plane_state(new_state);
+ struct st75256_device *st75256 = drm_to_st75256(plane->dev);
+ struct drm_framebuffer *fb = new_state->fb;
+ struct drm_rect damage;
+ int idx, ret;
+ u8 mode;
+
+ if (!fb || !new_state->visible)
+ return;
+
+ if (!drm_dev_enter(plane->dev, &idx))
+ return;
+
+ mode = fb->format->format == DRM_FORMAT_R1 ?
+ ST75256_MODE_MONO : ST75256_MODE_GRAY;
+
+ if (mode != st75256->hw_mode) {
+ ret = st75256_set_mode(st75256, mode);
+ if (ret)
+ goto err;
+ /* Display RAM layout changed, repaint everything */
+ drm_rect_fp_to_int(&damage, &new_state->src);
+ } else if (!drm_atomic_helper_damage_merged(old_state, new_state, &damage)) {
+ goto out;
+ }
+
+ ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE);
+ if (ret)
+ goto err;
+
+ ret = st75256_fb_blit(st75256, fb, &shadow_state->data[0], new_state,
+ &damage, &shadow_state->fmtcnv_state);
+
+ drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
+err:
+ if (ret)
+ drm_err_once(plane->dev, "Failed to update display: %d\n", ret);
+out:
+ drm_dev_exit(idx);
+}
+
+static void st75256_plane_atomic_disable(struct drm_plane *plane,
+ struct drm_atomic_commit *state)
+{
+ struct st75256_device *st75256 = drm_to_st75256(plane->dev);
+ struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state, plane);
+ struct drm_crtc_state *crtc_state;
+ int idx;
+
+ if (!old_state->crtc)
+ return;
+
+ crtc_state = drm_atomic_get_new_crtc_state(state, old_state->crtc);
+ if (!crtc_state || !crtc_state->active)
+ return;
+
+ if (!drm_dev_enter(plane->dev, &idx))
+ return;
+
+ st75256_clear(st75256);
+
+ drm_dev_exit(idx);
+}
+
+static const struct drm_plane_helper_funcs st75256_plane_helper_funcs = {
+ DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
+ .atomic_check = st75256_plane_atomic_check,
+ .atomic_update = st75256_plane_atomic_update,
+ .atomic_disable = st75256_plane_atomic_disable,
+};
+
+static const struct drm_plane_funcs st75256_plane_funcs = {
+ .update_plane = drm_atomic_helper_update_plane,
+ .disable_plane = drm_atomic_helper_disable_plane,
+ .destroy = drm_plane_cleanup,
+ DRM_GEM_SHADOW_PLANE_FUNCS,
+};
+
+static enum drm_mode_status st75256_crtc_mode_valid(struct drm_crtc *crtc,
+ const struct drm_display_mode *mode)
+{
+ struct st75256_device *st75256 = drm_to_st75256(crtc->dev);
+
+ return drm_crtc_helper_mode_valid_fixed(crtc, mode, &st75256->mode);
+}
+
+static void st75256_crtc_atomic_enable(struct drm_crtc *crtc,
+ struct drm_atomic_commit *state)
+{
+ struct st75256_device *st75256 = drm_to_st75256(crtc->dev);
+ int idx, ret;
+
+ if (!drm_dev_enter(crtc->dev, &idx))
+ return;
+
+ ret = st75256_hw_init(st75256);
+ if (ret) {
+ drm_err(crtc->dev, "Failed to initialize display: %d\n", ret);
+ goto out;
+ }
+
+ backlight_enable(st75256->backlight);
+out:
+ drm_dev_exit(idx);
+}
+
+static void st75256_crtc_atomic_disable(struct drm_crtc *crtc,
+ struct drm_atomic_commit *state)
+{
+ struct st75256_device *st75256 = drm_to_st75256(crtc->dev);
+ int idx;
+
+ if (!drm_dev_enter(crtc->dev, &idx))
+ return;
+
+ backlight_disable(st75256->backlight);
+ st75256_hw_fini(st75256);
+
+ drm_dev_exit(idx);
+}
+
+static const struct drm_crtc_helper_funcs st75256_crtc_helper_funcs = {
+ .mode_valid = st75256_crtc_mode_valid,
+ .atomic_check = drm_crtc_helper_atomic_check,
+ .atomic_enable = st75256_crtc_atomic_enable,
+ .atomic_disable = st75256_crtc_atomic_disable,
+};
+
+static const struct drm_crtc_funcs st75256_crtc_funcs = {
+ .atomic_create_state = drm_atomic_helper_crtc_create_state,
+ .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
+ .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
+ .set_config = drm_atomic_helper_set_config,
+ .page_flip = drm_atomic_helper_page_flip,
+ .destroy = drm_crtc_cleanup,
+};
+
+static const struct drm_encoder_funcs st75256_encoder_funcs = {
+ .destroy = drm_encoder_cleanup,
+};
+
+static int st75256_connector_get_modes(struct drm_connector *connector)
+{
+ struct st75256_device *st75256 = drm_to_st75256(connector->dev);
+
+ return drm_connector_helper_get_modes_fixed(connector, &st75256->mode);
+}
+
+static const struct drm_connector_helper_funcs st75256_connector_helper_funcs = {
+ .get_modes = st75256_connector_get_modes,
+};
+
+static const struct drm_connector_funcs st75256_connector_funcs = {
+ .fill_modes = drm_helper_probe_single_connector_modes,
+ .destroy = drm_connector_cleanup,
+ .atomic_create_state = drm_atomic_helper_connector_create_state,
+ .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
+ .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
+};
+
+static const struct drm_mode_config_funcs st75256_mode_config_funcs = {
+ .fb_create = drm_gem_fb_create_with_dirty,
+ .atomic_check = drm_atomic_helper_check,
+ .atomic_commit = drm_atomic_helper_commit,
+};
+
+/*
+ * The controller is initialized in the CRTC enable, so the CRTC has to be
+ * enabled before the plane is updated.
+ */
+static const struct drm_mode_config_helper_funcs st75256_mode_config_helper_funcs = {
+ .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
+};
+
+DEFINE_DRM_GEM_FOPS(st75256_fops);
+
+static const struct drm_driver st75256_drm_driver = {
+ DRM_GEM_SHMEM_DRIVER_OPS,
+ DRM_FBDEV_SHMEM_DRIVER_OPS,
+ .name = DRIVER_NAME,
+ .desc = DRIVER_DESC,
+ .major = DRIVER_MAJOR,
+ .minor = DRIVER_MINOR,
+ .driver_features = DRIVER_ATOMIC | DRIVER_GEM | DRIVER_MODESET,
+ .fops = &st75256_fops,
+};
+
+static int st75256_modeset_init(struct st75256_device *st75256)
+{
+ const struct st75256_panel *panel = &st75256->panel;
+ struct drm_device *drm = &st75256->drm;
+ const struct drm_display_mode mode = {
+ DRM_SIMPLE_MODE(panel->width, panel->height,
+ panel->width_mm, panel->height_mm),
+ };
+ int ret;
+
+ ret = drmm_mode_config_init(drm);
+ if (ret)
+ return ret;
+
+ drm_mode_copy(&st75256->mode, &mode);
+
+ drm->mode_config.min_width = mode.hdisplay;
+ drm->mode_config.max_width = max_t(unsigned int, mode.hdisplay,
+ DRM_SHADOW_PLANE_MAX_WIDTH);
+ drm->mode_config.min_height = mode.vdisplay;
+ drm->mode_config.max_height = max_t(unsigned int, mode.vdisplay,
+ DRM_SHADOW_PLANE_MAX_HEIGHT);
+ drm->mode_config.preferred_depth = 24;
+ drm->mode_config.funcs = &st75256_mode_config_funcs;
+ drm->mode_config.helper_private = &st75256_mode_config_helper_funcs;
+
+ ret = drm_universal_plane_init(drm, &st75256->plane, 0,
+ &st75256_plane_funcs,
+ st75256_formats, ARRAY_SIZE(st75256_formats),
+ NULL, DRM_PLANE_TYPE_PRIMARY, NULL);
+ if (ret)
+ return ret;
+
+ drm_plane_helper_add(&st75256->plane, &st75256_plane_helper_funcs);
+ drm_plane_enable_fb_damage_clips(&st75256->plane);
+
+ ret = drm_crtc_init_with_planes(drm, &st75256->crtc, &st75256->plane,
+ NULL, &st75256_crtc_funcs, NULL);
+ if (ret)
+ return ret;
+
+ drm_crtc_helper_add(&st75256->crtc, &st75256_crtc_helper_funcs);
+
+ ret = drm_encoder_init(drm, &st75256->encoder, &st75256_encoder_funcs,
+ DRM_MODE_ENCODER_NONE, NULL);
+ if (ret)
+ return ret;
+
+ st75256->encoder.possible_crtcs = drm_crtc_mask(&st75256->crtc);
+
+ ret = drm_connector_init(drm, &st75256->connector,
+ &st75256_connector_funcs, DRM_MODE_CONNECTOR_SPI);
+ if (ret)
+ return ret;
+
+ drm_connector_helper_add(&st75256->connector,
+ &st75256_connector_helper_funcs);
+
+ ret = drm_connector_attach_encoder(&st75256->connector,
+ &st75256->encoder);
+ if (ret)
+ return ret;
+
+ return drm_mode_config_create_initial_state(drm);
+}
+
+static int st75256_parse_dt(struct st75256_device *st75256)
+{
+ struct st75256_panel *panel = &st75256->panel;
+ struct device *dev = &st75256->spi->dev;
+ u32 gray[2] = { ST75256_GRAY_LIGHT_DEFAULT, ST75256_GRAY_DARK_DEFAULT };
+ struct display_timing dt;
+ u32 vop;
+ int ret;
+
+ ret = of_get_display_timing(dev->of_node, "panel-timing", &dt);
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to get panel-timing\n");
+
+ if (!dt.hactive.typ || dt.hactive.typ > ST75256_MAX_WIDTH ||
+ dt.hactive.typ % 8)
+ return dev_err_probe(dev, -EINVAL,
+ "hactive %u must be a multiple of 8 up to %u\n",
+ dt.hactive.typ, ST75256_MAX_WIDTH);
+
+ if (!dt.vactive.typ || dt.vactive.typ > ST75256_MAX_HEIGHT)
+ return dev_err_probe(dev, -EINVAL,
+ "vactive %u out of range 1-%u\n",
+ dt.vactive.typ, ST75256_MAX_HEIGHT);
+
+ panel->width = dt.hactive.typ;
+ panel->height = dt.vactive.typ;
+
+ ret = device_property_read_u32(dev, "width-mm", &panel->width_mm);
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to read width-mm\n");
+
+ ret = device_property_read_u32(dev, "height-mm", &panel->height_mm);
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to read height-mm\n");
+
+ ret = device_property_read_u32(dev, "sitronix,vop", &vop);
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to read sitronix,vop\n");
+
+ if (vop < ST75256_VOP_MIN || vop > ST75256_VOP_MAX)
+ return dev_err_probe(dev, -EINVAL,
+ "sitronix,vop %u out of range %u-%u\n",
+ vop, ST75256_VOP_MIN, ST75256_VOP_MAX);
+
+ panel->vop = vop;
+
+ ret = device_property_read_u32_array(dev, "sitronix,gray-levels",
+ gray, ARRAY_SIZE(gray));
+ if (ret && ret != -EINVAL)
+ return dev_err_probe(dev, ret,
+ "Failed to read sitronix,gray-levels\n");
+
+ if (gray[0] > ST75256_GRAY_LEVEL_MAX || gray[1] > ST75256_GRAY_LEVEL_MAX)
+ return dev_err_probe(dev, -EINVAL,
+ "sitronix,gray-levels out of range 0-%u\n",
+ ST75256_GRAY_LEVEL_MAX);
+
+ panel->gray_light = gray[0];
+ panel->gray_dark = gray[1];
+
+ return 0;
+}
+
+static int st75256_probe(struct spi_device *spi)
+{
+ struct device *dev = &spi->dev;
+ const struct st75256_panel *panel;
+ struct st75256_device *st75256;
+ struct drm_device *drm;
+ int ret;
+
+ st75256 = devm_drm_dev_alloc(dev, &st75256_drm_driver,
+ struct st75256_device, drm);
+ if (IS_ERR(st75256))
+ return PTR_ERR(st75256);
+
+ drm = &st75256->drm;
+ st75256->spi = spi;
+ panel = &st75256->panel;
+
+ ret = st75256_parse_dt(st75256);
+ if (ret)
+ return ret;
+
+ st75256->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
+ if (IS_ERR(st75256->reset))
+ return dev_err_probe(dev, PTR_ERR(st75256->reset),
+ "Failed to get reset GPIO\n");
+
+ st75256->dc = devm_gpiod_get(dev, "dc", GPIOD_OUT_LOW);
+ if (IS_ERR(st75256->dc))
+ return dev_err_probe(dev, PTR_ERR(st75256->dc),
+ "Failed to get D/C GPIO\n");
+
+ st75256->backlight = devm_of_find_backlight(dev);
+ if (IS_ERR(st75256->backlight))
+ return dev_err_probe(dev, PTR_ERR(st75256->backlight),
+ "Failed to get backlight\n");
+
+ st75256->cmd_buf = devm_kmalloc(dev, ST75256_CMD_BUF_LEN, GFP_KERNEL);
+ /* Full frame in grayscale mode, the larger of the two */
+ st75256->tx_buf = devm_kmalloc(dev, panel->width / 4 * panel->height,
+ GFP_KERNEL);
+ st75256->line_buf = devm_kmalloc(dev, panel->width, GFP_KERNEL);
+ if (!st75256->cmd_buf || !st75256->tx_buf || !st75256->line_buf)
+ return -ENOMEM;
+
+ spi_set_drvdata(spi, st75256);
+
+ ret = st75256_modeset_init(st75256);
+ if (ret)
+ return ret;
+
+ ret = drm_dev_register(drm, 0);
+ if (ret)
+ return ret;
+
+ drm_client_setup(drm, NULL);
+
+ return 0;
+}
+
+static void st75256_remove(struct spi_device *spi)
+{
+ struct st75256_device *st75256 = spi_get_drvdata(spi);
+
+ drm_dev_unplug(&st75256->drm);
+ drm_atomic_helper_shutdown(&st75256->drm);
+}
+
+static void st75256_shutdown(struct spi_device *spi)
+{
+ struct st75256_device *st75256 = spi_get_drvdata(spi);
+
+ drm_atomic_helper_shutdown(&st75256->drm);
+}
+
+static const struct of_device_id st75256_of_match[] = {
+ { .compatible = "sitronix,st75256" },
+ { }
+};
+MODULE_DEVICE_TABLE(of, st75256_of_match);
+
+static const struct spi_device_id st75256_spi_id[] = {
+ { "st75256" },
+ { }
+};
+MODULE_DEVICE_TABLE(spi, st75256_spi_id);
+
+static struct spi_driver st75256_spi_driver = {
+ .driver = {
+ .name = DRIVER_NAME,
+ .of_match_table = st75256_of_match,
+ },
+ .id_table = st75256_spi_id,
+ .probe = st75256_probe,
+ .remove = st75256_remove,
+ .shutdown = st75256_shutdown,
+};
+module_spi_driver(st75256_spi_driver);
+
+MODULE_DESCRIPTION(DRIVER_DESC);
+MODULE_AUTHOR("Cyril Hrubis <metan@ucw.cz>");
+MODULE_LICENSE("GPL");
--
2.51.0
^ permalink raw reply related [flat|nested] 5+ messages in thread