main_i2c.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Drives the real panel directly through /dev/i2c-N, with no kernel driver.
  3. * This is the bring-up tool (--pattern), and the way to run on a kernel too
  4. * old for the DRM driver.
  5. *
  6. * Writing a raw byte stream to the wrong bus can reach anything attached to
  7. * it, so the adapter is checked by name before the first transfer.
  8. */
  9. #include <errno.h>
  10. #include <fcntl.h>
  11. #include <linux/i2c-dev.h>
  12. #include <linux/i2c.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <sys/ioctl.h>
  17. #include <unistd.h>
  18. #include "platform_linux.h"
  19. #include "s1d13700_core.h"
  20. #define TX_CAP 1025
  21. /* Never, whatever the options: motherboard and display buses */
  22. static const char *const denied_adapters[] = { "PIIX4", "AMDGPU", "DPMST", "i801", "SMBus" };
  23. /* Known to be a dedicated bus for this panel */
  24. static const char *const known_adapters[] = { "MCP2221", "FT260", "i2c-tiny-usb", "mv64xxx" };
  25. struct panel_dev {
  26. int fd;
  27. unsigned int addr;
  28. struct s1d_config cfg;
  29. struct s1d_panel panel;
  30. uint8_t shadow[S1D_VRAM_SIZE];
  31. uint8_t next[S1D_VRAM_SIZE];
  32. uint8_t tx[TX_CAP];
  33. const char *backlight;
  34. };
  35. static int dev_xfer(void *ctx, const uint8_t *buf, size_t len)
  36. {
  37. struct panel_dev *d = ctx;
  38. struct i2c_msg msg = { d->addr, 0, (uint16_t)len, (uint8_t *)buf };
  39. struct i2c_rdwr_ioctl_data data = { &msg, 1 };
  40. return ioctl(d->fd, I2C_RDWR, &data) < 0 ? -errno : 0;
  41. }
  42. static void dev_delay(void *ctx, unsigned int ms)
  43. {
  44. (void)ctx;
  45. usleep(ms * 1000);
  46. }
  47. static const struct s1d_ops dev_ops = { dev_xfer, dev_delay };
  48. static void present(void *ctx, const uint32_t *pixels, int stride, int x, int y, int w, int h)
  49. {
  50. struct panel_dev *d = ctx;
  51. int err;
  52. s1d_convert_xrgb8888(&d->cfg, pixels, stride, d->next, x, y, x + w, y + h);
  53. err = s1d_panel_update(&d->panel, d->next, y, y + h);
  54. if (err)
  55. g_printerr("panel update failed: %s\n", strerror(-err));
  56. }
  57. /* sysfs if a path was given, otherwise the pin on the panel's own expander */
  58. static void backlight(void *ctx, bool on)
  59. {
  60. struct panel_dev *d = ctx;
  61. int err;
  62. if (d->backlight) {
  63. platform_backlight_set(d->backlight, on);
  64. return;
  65. }
  66. err = s1d_panel_backlight(&d->panel, on);
  67. if (err)
  68. g_printerr("backlight: %s\n", strerror(-err));
  69. }
  70. static int open_bus(int bus, const char *allow)
  71. {
  72. char path[64], name[128] = "";
  73. unsigned long funcs = 0;
  74. unsigned int i;
  75. bool ok = false;
  76. FILE *f;
  77. int fd;
  78. snprintf(path, sizeof(path), "/sys/class/i2c-dev/i2c-%d/name", bus);
  79. f = fopen(path, "r");
  80. if (!f || !fgets(name, sizeof(name), f)) {
  81. g_printerr("i2c-%d: cannot read the adapter name\n", bus);
  82. if (f)
  83. fclose(f);
  84. return -1;
  85. }
  86. fclose(f);
  87. name[strcspn(name, "\n")] = '\0';
  88. for (i = 0; i < G_N_ELEMENTS(denied_adapters); i++) {
  89. if (strstr(name, denied_adapters[i])) {
  90. g_printerr("i2c-%d (%s): refusing, this is not a panel bus\n", bus, name);
  91. return -1;
  92. }
  93. }
  94. for (i = 0; i < G_N_ELEMENTS(known_adapters); i++)
  95. if (strstr(name, known_adapters[i]))
  96. ok = true;
  97. if (allow && !strcmp(allow, name))
  98. ok = true;
  99. if (!ok) {
  100. g_printerr("i2c-%d (%s): unknown adapter; if it really is the panel's bus,\n"
  101. "pass --allow-adapter=\"%s\"\n", bus, name, name);
  102. return -1;
  103. }
  104. snprintf(path, sizeof(path), "/dev/i2c-%d", bus);
  105. fd = open(path, O_RDWR | O_CLOEXEC);
  106. if (fd < 0) {
  107. g_printerr("%s: %s\n", path, strerror(errno));
  108. return -1;
  109. }
  110. if (ioctl(fd, I2C_FUNCS, &funcs) < 0 || !(funcs & I2C_FUNC_I2C)) {
  111. g_printerr("i2c-%d (%s): no raw I2C support\n", bus, name);
  112. close(fd);
  113. return -1;
  114. }
  115. printf("using i2c-%d (%s)\n", bus, name);
  116. return fd;
  117. }
  118. /* ---- bring-up patterns -------------------------------------------------- */
  119. static void put(uint32_t *fb, unsigned int w, unsigned int x, unsigned int y, uint32_t c)
  120. {
  121. fb[y * w + x] = c;
  122. }
  123. /* Border, plus ticks every 10 px and long ticks every 50: shows the real size */
  124. static void pattern_geometry(uint32_t *fb, unsigned int w, unsigned int h)
  125. {
  126. unsigned int x, y, i;
  127. for (x = 0; x < w; x++) {
  128. put(fb, w, x, 0, 0);
  129. put(fb, w, x, h - 1, 0);
  130. if (!(x % 10))
  131. for (i = 0; i < (x % 50 ? 4u : 10u); i++)
  132. put(fb, w, x, 1 + i, 0);
  133. }
  134. for (y = 0; y < h; y++) {
  135. put(fb, w, 0, y, 0);
  136. put(fb, w, w - 1, y, 0);
  137. if (!(y % 10))
  138. for (i = 0; i < (y % 50 ? 4u : 10u); i++)
  139. put(fb, w, 1 + i, y, 0);
  140. }
  141. /* a diagonal shows skew from a wrong line length */
  142. for (i = 0; i < h && i < w; i++)
  143. put(fb, w, i, i, 0);
  144. }
  145. static void pattern_gray(uint32_t *fb, unsigned int w, unsigned int h)
  146. {
  147. static const uint32_t levels[] = { 0xffffff, 0xaaaaaa, 0x555555, 0x000000 };
  148. unsigned int x, y;
  149. for (y = 0; y < h; y++)
  150. for (x = 0; x < w; x++)
  151. put(fb, w, x, y, levels[x * 4 / w]);
  152. }
  153. static int show_pattern(struct panel_dev *d, const char *name)
  154. {
  155. unsigned int w = d->cfg.width, h = d->cfg.height;
  156. uint32_t *fb = g_malloc((gsize)w * h * 4);
  157. int err;
  158. memset(fb, 0xff, (gsize)w * h * 4);
  159. if (!strcmp(name, "geometry")) {
  160. pattern_geometry(fb, w, h);
  161. } else if (!strcmp(name, "gray")) {
  162. pattern_gray(fb, w, h);
  163. } else {
  164. g_printerr("unknown pattern '%s' (geometry, gray)\n", name);
  165. g_free(fb);
  166. return 1;
  167. }
  168. s1d_convert_xrgb8888(&d->cfg, fb, w * 4, d->next, 0, 0, w, h);
  169. err = s1d_panel_update(&d->panel, d->next, 0, h);
  170. g_free(fb);
  171. if (err) {
  172. g_printerr("pattern failed: %s\n", strerror(-err));
  173. return 1;
  174. }
  175. printf("pattern sent: %lu I2C bytes in %lu messages\n",
  176. d->panel.stats.i2c_bytes, d->panel.stats.i2c_msgs);
  177. return 0;
  178. }
  179. int main(int argc, char **argv)
  180. {
  181. static struct panel_dev d;
  182. static int bus = -1, addr = 0x74, port = 6600;
  183. static int width = 240, height = 160, bpp = 2, tcr = 162, tx_cap = TX_CAP;
  184. static gboolean no_backlight;
  185. static char *host, *pattern, *allow, *input, *blpath;
  186. static gboolean kmsg;
  187. static GOptionEntry entries[] = {
  188. { "bus", 0, 0, G_OPTION_ARG_INT, &bus, "I2C bus number (required)", "N" },
  189. { "addr", 0, 0, G_OPTION_ARG_INT, &addr, "Expander address (0x74 = 116)", "N" },
  190. { "allow-adapter", 0, 0, G_OPTION_ARG_STRING, &allow, "Accept the adapter with this exact name", "NAME" },
  191. { "width", 0, 0, G_OPTION_ARG_INT, &width, "Panel width (240)", "PX" },
  192. { "height", 0, 0, G_OPTION_ARG_INT, &height, "Panel height (160)", "PX" },
  193. { "bpp", 0, 0, G_OPTION_ARG_INT, &bpp, "Bits per pixel: 1, 2, 4 (2)", "N" },
  194. { "tcr", 0, 0, G_OPTION_ARG_INT, &tcr, "TC/R, sets the frame rate (162)", "N" },
  195. { "tx-cap", 0, 0, G_OPTION_ARG_INT, &tx_cap, "Largest I2C message, 7.." G_STRINGIFY(TX_CAP) " (" G_STRINGIFY(TX_CAP) ")", "BYTES" },
  196. { "no-backlight", 0, 0, G_OPTION_ARG_NONE, &no_backlight, "Leave the expander's backlight pin off", NULL },
  197. { "pattern", 0, 0, G_OPTION_ARG_STRING, &pattern, "Show a test pattern and exit: geometry, gray", "NAME" },
  198. { "host", 0, 0, G_OPTION_ARG_STRING, &host, "MPD host (localhost)", "NAME" },
  199. { "port", 0, 0, G_OPTION_ARG_INT, &port, "MPD port (6600)", "N" },
  200. { "input", 0, 0, G_OPTION_ARG_FILENAME, &input, "Key input device (auto: " DEFAULT_KEYS_NAME ")", "PATH" },
  201. { "backlight", 0, 0, G_OPTION_ARG_FILENAME, &blpath, "Backlight sysfs brightness file (default: expander pin P1.0)", "PATH" },
  202. { "kmsg", 0, 0, G_OPTION_ARG_NONE, &kmsg, "Show the kernel log", NULL },
  203. { NULL, 0, 0, 0, NULL, NULL, NULL },
  204. };
  205. GOptionContext *ctx = g_option_context_new("- radio panel over i2c-dev");
  206. struct app_frontend fe = { &d, present, backlight };
  207. struct app_options opt;
  208. GError *err = NULL;
  209. struct app *app;
  210. int ret;
  211. g_option_context_add_main_entries(ctx, entries, NULL);
  212. if (!g_option_context_parse(ctx, &argc, &argv, &err)) {
  213. g_printerr("%s\n", err->message);
  214. return 1;
  215. }
  216. if (bus < 0) {
  217. g_printerr("--bus is required\n");
  218. return 1;
  219. }
  220. d.fd = open_bus(bus, allow);
  221. if (d.fd < 0)
  222. return 1;
  223. d.addr = addr;
  224. d.cfg = (struct s1d_config){ width, height, bpp, tcr };
  225. d.backlight = blpath;
  226. if (tx_cap < 0 || tx_cap > TX_CAP) {
  227. g_printerr("--tx-cap: at most %d\n", TX_CAP);
  228. return 1;
  229. }
  230. ret = s1d_panel_setup(&d.panel, &d.cfg, &dev_ops, &d, d.shadow, d.tx, tx_cap);
  231. if (ret) {
  232. g_printerr("invalid panel configuration\n");
  233. return 1;
  234. }
  235. if (!blpath && !no_backlight)
  236. s1d_panel_backlight(&d.panel, true);
  237. ret = s1d_panel_init(&d.panel);
  238. if (ret) {
  239. g_printerr("panel init failed: %s\n", strerror(-ret));
  240. return 1;
  241. }
  242. if (pattern)
  243. return show_pattern(&d, pattern);
  244. opt = (struct app_options){ host ? host : "localhost", port, width, height, kmsg };
  245. app = app_new(&opt, &fe);
  246. if (!platform_keys_start(app, input))
  247. g_printerr("no key input device, running display-only\n");
  248. platform_run();
  249. app_free(app);
  250. s1d_panel_display(&d.panel, false);
  251. return 0;
  252. }