# s1d13700-tca: S1D13700 LCD behind a TCA9539 I²C expander A Linux DRM driver for an Epson S1D13700 (SED1335 family) graphic LCD whose 8080-style parallel bus is driven through a TI TCA9539 16-bit I²C GPIO expander, plus the parts that make it testable without hardware: - an OS-independent **panel core** (`driver/s1d13700_core.*`) that the driver uses and userspace can link as well, - a **simulator** (`sim/`) that decodes the I²C byte stream as a TCA9539 and S1D13700 would and flags bus protocol faults, - **tests** for the core, run against the simulator, - **device tree** examples: an overlay and a board DT fragment. It was written for a 240×160 panel at 2 bpp on an Orange Pi Zero (Allwinner H2+, 400 kHz I²C) and runs there, built into the kernel, with fbcon on the panel. The radio-ui application (sibling repository `radio-ui`) uses the same core from userspace, over i2c-dev or through this driver. | Path | What | |---|---| | `driver/s1d13700_core.*` | Panel core: init, XRGB8888 to 1/2/4 bpp, write planner, TCA9539 stream encoder | | `driver/s1d13700_drm.c` | Atomic KMS driver with fbdev emulation and a backlight device | | `driver/Kbuild` | Out-of-tree build | | `sim/` | Model of the TCA9539 and S1D13700 interface, with fault counters | | `tests/test_panel.c` | Core tests against the simulator | | `dts/radio-panel.dts` | Overlay: the panel | | `dts/examples/orangepi-zero-i2c1.dtsi` | The same for a board DT, as used on the Orange Pi Zero | | `docs/s1d13700-recommendations.md` | Review of the core and driver, applied | ## Building make test # core tests, no hardware or libraries make asan # the same under ASan and UBSan make module KDIR=/path/to/kernel make dtbo # build/*.dtbo from dts/*.dts The driver needs a 6.x kernel with damage helpers, `drm_fbdev_shmem` and the DRM client setup (built against 6.18 and 7.3). It does not build on 4.19; there the panel can still be driven from userspace through i2c-dev with the core. ### In the kernel tree Copy `driver/s1d13700_drm.c`, `s1d13700_core.c` and `s1d13700_core.h` into e.g. `drivers/gpu/drm/tiny/s1d13700-tca/` and add: # Kconfig config DRM_S1D13700_TCA tristate "S1D13700 panel behind a TCA9539 I2C expander" depends on DRM && I2C select DRM_CLIENT_SELECTION select DRM_KMS_HELPER select DRM_GEM_SHMEM_HELPER select BACKLIGHT_CLASS_DEVICE # Makefile obj-$(CONFIG_DRM_S1D13700_TCA) += s1d13700-tca.o s1d13700-tca-y := s1d13700_drm.o s1d13700_core.o The Orange Pi Zero build scripts (`build-kernel.sh` in the OPi0 project) do this on every build and set it to built-in, so fbcon shows the boot log on the panel. Linux 7.3 renamed `struct drm_atomic_state` to `struct drm_atomic_commit`; that script force-includes a small compatibility header into `s1d13700_drm.c` rather than changing the source. ## Driver - **Binding:** I²C client, `compatible = "k4be,s1d13700-tca9539"` (`k4be` is not a registered vendor prefix, so `dtbs_check` warns), or I²C id `s1d13700-tca9539` for `new_device`: echo s1d13700-tca9539 0x74 > /sys/bus/i2c/devices/i2c-N/new_device - **Module parameters** (read-only, so kernel arguments when built in, e.g. `s1d13700_tca.tcr=162`): | Parameter | Default | | |---|---|---| | `width`, `height` | 240, 160 | panel size in pixels | | `bpp` | 2 | 1, 2 or 4 bits per pixel | | `tcr` | 162 | SYSTEM SET TC/R, sets the frame rate | - **Format:** XRGB8888, converted to gray with a plain threshold (no dithering). White maps to "pixel off", the light background of the LCD. - **Updates:** plane updates turn damage into a pending frame; a worker sends it and merges whatever arrives meanwhile. Messages are capped at 1025 bytes (about 23 ms at 400 kHz) so other devices on the bus get a turn, and the adapter's `max_write_len` is honoured. A failed transfer makes the next update rewrite the frame; a failed init is retried. - **Backlight:** expander pin P1.0 is registered as `/sys/class/backlight/radio-backlight` (brightness 0 or 1, on at probe). The sysfs file is `root:root 0644`; a udev rule can open it to a group: ACTION=="add", SUBSYSTEM=="backlight", KERNEL=="radio-backlight", RUN+="/bin/chgrp video /sys%p/brightness", RUN+="/bin/chmod g+w /sys%p/brightness" - **Statistics** in debugfs, under the I²C client's directory: `lcd_writes`, `data_bytes`, `seeks`, `dir_changes`, `i2c_msgs`, `i2c_bytes`. - Blanking is display off only; the controller's `SLEEP` is not used. Only one driver can own an I²C address: U1 must not be bound to `gpio-pca953x`. The driver needs one continuous write that alternates both expander ports, which the GPIO API cannot express (going through it would cost roughly 2.5-3× more bus time per LCD byte). ## Wiring **U1, TCA9539 at 0x74** (A1=L, A0=L), the driver's I²C client: | Pin | Signal | Module pin | |---|---|---| | P0.0-7 | D0-D7 | D0-D7 | | P1.0 | Backlight enable (active high) | | | P1.1 | RES# | RST | | P1.2 | A0 | A15 | | P1.3 | WR# | WE | | P1.4 | RD# | RE | | P1.5 | CS# (held low after reset) | CS0 | | P1.6-7 | spare | | The pin assignment is fixed in `s1d13700_core.h`. The keys on the same bus (U2, a second TCA9539 at 0x76, with stock `gpio-pca953x` and `gpio-keys`) are in the sibling repository [radio-keyboard-tca](../radio-keyboard-tca): wiring, key map and device tree. ## Device tree `dts/radio-panel.dts` is an overlay written with plain numbers instead of `#include`, so it also works with `armbian-add-overlay`. It targets `i2c0`; check that against the board. For the i2c-dev userspace path, do not load it. `dts/examples/orangepi-zero-i2c1.dtsi` is the version in use on the Orange Pi Zero, for inclusion in a board DTS: i2c1 at 400 kHz and the panel. The keys' overlay and board fragment are in radio-keyboard-tca. ## How it works ### Panel core - **Stream encoder.** One write message starts with command byte `0x02`, then alternates P0 (data) and P1 (control). Each LCD cycle is `data, WR=0, data, WR=1`. A0 changes get their own P0/P1 pair with WR# high, because A0 needs setup time before the WR# edge and shares a port with it. A cycle is never split across messages, so WR# is not left low when the bus is released. - **Init.** Programs the expander outputs before the directions, holds reset 20 ms, then configures the controller: `FX=7` and `HDOT=0` (required above 1 bpp), `CM=1` (required in graphics mode), grayscale value 1. VRAM is cleared before the display is turned on. - **Planner.** Compares each frame with a shadow of VRAM at byte level, so even a client that redraws everything costs only what changed. Rows with changes form bands, each written horizontally or vertically (`CSRDIR` down), whichever is cheaper. Unchanged gaps up to 6 bytes (the cost of a seek) are rewritten rather than skipped. - **Cursor.** A memory write turns the controller's cursor on even when the DISP ON attributes say off (REG[0Ah] note in the hardware specification). After any update that wrote VRAM while the display is on, the core re-sends DISP ON (12 I²C bytes). - **Transport.** The caller supplies a send function: `i2c_master_send()` in the driver, `I2C_RDWR` on i2c-dev, or the simulator. ### Simulator Models the TCA9539 register pairs and the S1D13700 indirect interface from the I²C bytes alone. It counts faults: A0 changing with a WR# edge, parameters with no command or too many, unknown commands, bad registers, messages ending between P0 and P1, inconsistent SYSTEM SET, and writes during reset. It also models the cursor behaviour above. ### Bus cost | Update (240×160, 2 bpp, 400 kHz) | I²C bytes | Bus time | |---|---|---| | One 8×13 digit | 169 | 3.8 ms | | Full frame | 38 474 (38 messages) | 867 ms | | Typical small UI update | 113-177 | 2.6-4.0 ms | Calculated from the bytes sent on an ideal bus; real timing is slower. The Allwinner `i2c-mv64xxx` takes about one interrupt per byte. ## Tests `make test` covers: init values, invalid configurations, no change means no traffic, direction choice, gap merging, 2000 random update rounds at 1/2/4 bpp and message sizes 7/61/1025, transport and backlight failure recovery, lost direction recovery, pixel conversion, the simulator's renderer and its fault detector. They also pass under ASan and UBSan. ## Not settled - TC/R and the frame rate: 162 comes from a working AVR 2 bpp setup. - Flicker: the datasheet recommends VRAM access during blanking, which cannot be synchronised over I²C. - The extra cycle for A0 setup may be removable, saving about a third of the seek cost. - Only XRGB8888 is exposed; R1/R2 formats could be added. - The backlight name and the U1 pin assignment are fixed in the source. ## References - [S1D13700F01 hardware specification, Rev. 4.07](https://global-origin.epson.com/products_and_drivers/semicon/pdf/id003161.pdf) - [TCA9539 datasheet (SCPS202C)](https://www.ti.com/lit/ds/symlink/tca9539.pdf) - In-tree template: `drivers/gpu/drm/sitronix/st7571-i2c.c` ## License GPL-2.0-only (see `LICENSE` and the SPDX headers).