/* * Drives the real panel directly through /dev/i2c-N, with no kernel driver. * This is the bring-up tool (--pattern), and the way to run on a kernel too * old for the DRM driver. * * Writing a raw byte stream to the wrong bus can reach anything attached to * it, so the adapter is checked by name before the first transfer. */ #include #include #include #include #include #include #include #include #include #include "platform_linux.h" #include "s1d13700_core.h" #define TX_CAP 1025 /* Never, whatever the options: motherboard and display buses */ static const char *const denied_adapters[] = { "PIIX4", "AMDGPU", "DPMST", "i801", "SMBus" }; /* Known to be a dedicated bus for this panel */ static const char *const known_adapters[] = { "MCP2221", "FT260", "i2c-tiny-usb", "mv64xxx" }; struct panel_dev { int fd; unsigned int addr; struct s1d_config cfg; struct s1d_panel panel; uint8_t shadow[S1D_VRAM_SIZE]; uint8_t next[S1D_VRAM_SIZE]; uint8_t tx[TX_CAP]; const char *backlight; }; static int dev_xfer(void *ctx, const uint8_t *buf, size_t len) { struct panel_dev *d = ctx; struct i2c_msg msg = { d->addr, 0, (uint16_t)len, (uint8_t *)buf }; struct i2c_rdwr_ioctl_data data = { &msg, 1 }; return ioctl(d->fd, I2C_RDWR, &data) < 0 ? -errno : 0; } static void dev_delay(void *ctx, unsigned int ms) { (void)ctx; usleep(ms * 1000); } static const struct s1d_ops dev_ops = { dev_xfer, dev_delay }; static void present(void *ctx, const uint32_t *pixels, int stride, int x, int y, int w, int h) { struct panel_dev *d = ctx; int err; s1d_convert_xrgb8888(&d->cfg, pixels, stride, d->next, x, y, x + w, y + h); err = s1d_panel_update(&d->panel, d->next, y, y + h); if (err) g_printerr("panel update failed: %s\n", strerror(-err)); } /* sysfs if a path was given, otherwise the pin on the panel's own expander */ static void backlight(void *ctx, bool on) { struct panel_dev *d = ctx; int err; if (d->backlight) { platform_backlight_set(d->backlight, on); return; } err = s1d_panel_backlight(&d->panel, on); if (err) g_printerr("backlight: %s\n", strerror(-err)); } static int open_bus(int bus, const char *allow) { char path[64], name[128] = ""; unsigned long funcs = 0; unsigned int i; bool ok = false; FILE *f; int fd; snprintf(path, sizeof(path), "/sys/class/i2c-dev/i2c-%d/name", bus); f = fopen(path, "r"); if (!f || !fgets(name, sizeof(name), f)) { g_printerr("i2c-%d: cannot read the adapter name\n", bus); if (f) fclose(f); return -1; } fclose(f); name[strcspn(name, "\n")] = '\0'; for (i = 0; i < G_N_ELEMENTS(denied_adapters); i++) { if (strstr(name, denied_adapters[i])) { g_printerr("i2c-%d (%s): refusing, this is not a panel bus\n", bus, name); return -1; } } for (i = 0; i < G_N_ELEMENTS(known_adapters); i++) if (strstr(name, known_adapters[i])) ok = true; if (allow && !strcmp(allow, name)) ok = true; if (!ok) { g_printerr("i2c-%d (%s): unknown adapter; if it really is the panel's bus,\n" "pass --allow-adapter=\"%s\"\n", bus, name, name); return -1; } snprintf(path, sizeof(path), "/dev/i2c-%d", bus); fd = open(path, O_RDWR | O_CLOEXEC); if (fd < 0) { g_printerr("%s: %s\n", path, strerror(errno)); return -1; } if (ioctl(fd, I2C_FUNCS, &funcs) < 0 || !(funcs & I2C_FUNC_I2C)) { g_printerr("i2c-%d (%s): no raw I2C support\n", bus, name); close(fd); return -1; } printf("using i2c-%d (%s)\n", bus, name); return fd; } /* ---- bring-up patterns -------------------------------------------------- */ static void put(uint32_t *fb, unsigned int w, unsigned int x, unsigned int y, uint32_t c) { fb[y * w + x] = c; } /* Border, plus ticks every 10 px and long ticks every 50: shows the real size */ static void pattern_geometry(uint32_t *fb, unsigned int w, unsigned int h) { unsigned int x, y, i; for (x = 0; x < w; x++) { put(fb, w, x, 0, 0); put(fb, w, x, h - 1, 0); if (!(x % 10)) for (i = 0; i < (x % 50 ? 4u : 10u); i++) put(fb, w, x, 1 + i, 0); } for (y = 0; y < h; y++) { put(fb, w, 0, y, 0); put(fb, w, w - 1, y, 0); if (!(y % 10)) for (i = 0; i < (y % 50 ? 4u : 10u); i++) put(fb, w, 1 + i, y, 0); } /* a diagonal shows skew from a wrong line length */ for (i = 0; i < h && i < w; i++) put(fb, w, i, i, 0); } static void pattern_gray(uint32_t *fb, unsigned int w, unsigned int h) { static const uint32_t levels[] = { 0xffffff, 0xaaaaaa, 0x555555, 0x000000 }; unsigned int x, y; for (y = 0; y < h; y++) for (x = 0; x < w; x++) put(fb, w, x, y, levels[x * 4 / w]); } static int show_pattern(struct panel_dev *d, const char *name) { unsigned int w = d->cfg.width, h = d->cfg.height; uint32_t *fb = g_malloc((gsize)w * h * 4); int err; memset(fb, 0xff, (gsize)w * h * 4); if (!strcmp(name, "geometry")) { pattern_geometry(fb, w, h); } else if (!strcmp(name, "gray")) { pattern_gray(fb, w, h); } else { g_printerr("unknown pattern '%s' (geometry, gray)\n", name); g_free(fb); return 1; } s1d_convert_xrgb8888(&d->cfg, fb, w * 4, d->next, 0, 0, w, h); err = s1d_panel_update(&d->panel, d->next, 0, h); g_free(fb); if (err) { g_printerr("pattern failed: %s\n", strerror(-err)); return 1; } printf("pattern sent: %lu I2C bytes in %lu messages\n", d->panel.stats.i2c_bytes, d->panel.stats.i2c_msgs); return 0; } int main(int argc, char **argv) { static struct panel_dev d; static int bus = -1, addr = 0x74, port = 6600; static int width = 240, height = 160, bpp = 2, tcr = 162, tx_cap = TX_CAP; static gboolean no_backlight; static char *host, *pattern, *allow, *input, *blpath; static gboolean kmsg; static GOptionEntry entries[] = { { "bus", 0, 0, G_OPTION_ARG_INT, &bus, "I2C bus number (required)", "N" }, { "addr", 0, 0, G_OPTION_ARG_INT, &addr, "Expander address (0x74 = 116)", "N" }, { "allow-adapter", 0, 0, G_OPTION_ARG_STRING, &allow, "Accept the adapter with this exact name", "NAME" }, { "width", 0, 0, G_OPTION_ARG_INT, &width, "Panel width (240)", "PX" }, { "height", 0, 0, G_OPTION_ARG_INT, &height, "Panel height (160)", "PX" }, { "bpp", 0, 0, G_OPTION_ARG_INT, &bpp, "Bits per pixel: 1, 2, 4 (2)", "N" }, { "tcr", 0, 0, G_OPTION_ARG_INT, &tcr, "TC/R, sets the frame rate (162)", "N" }, { "tx-cap", 0, 0, G_OPTION_ARG_INT, &tx_cap, "Largest I2C message, 7.." G_STRINGIFY(TX_CAP) " (" G_STRINGIFY(TX_CAP) ")", "BYTES" }, { "no-backlight", 0, 0, G_OPTION_ARG_NONE, &no_backlight, "Leave the expander's backlight pin off", NULL }, { "pattern", 0, 0, G_OPTION_ARG_STRING, &pattern, "Show a test pattern and exit: geometry, gray", "NAME" }, { "host", 0, 0, G_OPTION_ARG_STRING, &host, "MPD host (localhost)", "NAME" }, { "port", 0, 0, G_OPTION_ARG_INT, &port, "MPD port (6600)", "N" }, { "input", 0, 0, G_OPTION_ARG_FILENAME, &input, "Key input device (auto: " DEFAULT_KEYS_NAME ")", "PATH" }, { "backlight", 0, 0, G_OPTION_ARG_FILENAME, &blpath, "Backlight sysfs brightness file (default: expander pin P1.0)", "PATH" }, { "kmsg", 0, 0, G_OPTION_ARG_NONE, &kmsg, "Show the kernel log", NULL }, { NULL, 0, 0, 0, NULL, NULL, NULL }, }; GOptionContext *ctx = g_option_context_new("- radio panel over i2c-dev"); struct app_frontend fe = { &d, present, backlight }; struct app_options opt; GError *err = NULL; struct app *app; int ret; g_option_context_add_main_entries(ctx, entries, NULL); if (!g_option_context_parse(ctx, &argc, &argv, &err)) { g_printerr("%s\n", err->message); return 1; } if (bus < 0) { g_printerr("--bus is required\n"); return 1; } d.fd = open_bus(bus, allow); if (d.fd < 0) return 1; d.addr = addr; d.cfg = (struct s1d_config){ width, height, bpp, tcr }; d.backlight = blpath; if (tx_cap < 0 || tx_cap > TX_CAP) { g_printerr("--tx-cap: at most %d\n", TX_CAP); return 1; } ret = s1d_panel_setup(&d.panel, &d.cfg, &dev_ops, &d, d.shadow, d.tx, tx_cap); if (ret) { g_printerr("invalid panel configuration\n"); return 1; } if (!blpath && !no_backlight) s1d_panel_backlight(&d.panel, true); ret = s1d_panel_init(&d.panel); if (ret) { g_printerr("panel init failed: %s\n", strerror(-ret)); return 1; } if (pattern) return show_pattern(&d, pattern); opt = (struct app_options){ host ? host : "localhost", port, width, height, kmsg }; app = app_new(&opt, &fe); if (!platform_keys_start(app, input)) g_printerr("no key input device, running display-only\n"); platform_run(); app_free(app); s1d_panel_display(&d.panel, false); return 0; }