s1d13700_core.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * S1D13700 behind a TCA9539: hardware-independent panel core.
  4. *
  5. * Shared by the DRM driver, the i2c-dev frontend, the GTK simulator and the
  6. * tests. It owns everything between "new frame in native format" and "bytes
  7. * for one I2C write", and has no kernel or libc dependency beyond memcmp().
  8. *
  9. * Wire format: every write starts with the Output Port 0 command byte, then
  10. * alternates P0 (LCD data) and P1 (LCD control), relying on the TCA9539
  11. * staying within the addressed register pair. Each transfer therefore holds
  12. * an odd number of bytes and always restarts at P0.
  13. */
  14. #ifndef S1D13700_CORE_H
  15. #define S1D13700_CORE_H
  16. #ifdef __KERNEL__
  17. #include <linux/types.h>
  18. #else
  19. #include <stdbool.h>
  20. #include <stddef.h>
  21. #include <stdint.h>
  22. #endif
  23. /* TCA9539 command bytes */
  24. #define TCA_REG_OUTPUT0 0x02
  25. #define TCA_REG_POLARITY0 0x04
  26. #define TCA_REG_CONFIG0 0x06
  27. /* U1 port 1: LCD control lines (module pin names in brackets) */
  28. #define S1D_P1_BL 0x01 /* backlight enable, active high */
  29. #define S1D_P1_RES 0x02 /* RES# [RST] */
  30. #define S1D_P1_A0 0x04 /* [A15]: 1 = command, 0 = parameter/data */
  31. #define S1D_P1_WR 0x08 /* WR# [WE] */
  32. #define S1D_P1_RD 0x10 /* RD# [RE] */
  33. #define S1D_P1_CS 0x20 /* CS# [CS0] */
  34. #define S1D_P1_CONFIG 0xc0 /* P1.6-7 unused: stay inputs */
  35. /* S1D13700 indirect-mode commands */
  36. #define S1D_CMD_SYSTEM_SET 0x40
  37. #define S1D_CMD_MWRITE 0x42
  38. #define S1D_CMD_MREAD 0x43
  39. #define S1D_CMD_SCROLL 0x44
  40. #define S1D_CMD_CSRW 0x46
  41. #define S1D_CMD_CSRR 0x47
  42. #define S1D_CMD_CSRDIR_R 0x4c
  43. #define S1D_CMD_CSRDIR_L 0x4d
  44. #define S1D_CMD_CSRDIR_U 0x4e
  45. #define S1D_CMD_CSRDIR_D 0x4f
  46. #define S1D_CMD_SLEEP 0x53
  47. #define S1D_CMD_DISP_OFF 0x58
  48. #define S1D_CMD_DISP_ON 0x59
  49. #define S1D_CMD_HDOT_SCR 0x5a
  50. #define S1D_CMD_OVLAY 0x5b
  51. #define S1D_CMD_CGRAM_ADR 0x5c
  52. #define S1D_CMD_CSRFORM 0x5d
  53. #define S1D_CMD_GRAYSCALE 0x60
  54. #define S1D_VRAM_SIZE 32768u
  55. /*
  56. * Rewriting an unchanged gap costs one LCD write per byte; seeking costs
  57. * CSRW + 2 parameters + MWRITE (4 writes, 16 I2C bytes) plus four A0
  58. * transitions of one extra P0/P1 pair each (8 I2C bytes). That is 24 I2C
  59. * bytes, the same as 6 data bytes.
  60. */
  61. #define S1D_SEEK_COST 6u
  62. #define S1D_DIR_COST 1u
  63. struct s1d_config {
  64. unsigned int width; /* pixels, multiple of the pixels in one byte */
  65. unsigned int height;
  66. unsigned int bpp; /* 1, 2 or 4 */
  67. unsigned int tcr; /* SYSTEM SET TC/R: tune for frame rate */
  68. };
  69. struct s1d_ops {
  70. /* buf[0] is the TCA9539 command byte; the transport adds the address */
  71. int (*xfer)(void *ctx, const uint8_t *buf, size_t len);
  72. void (*delay_ms)(void *ctx, unsigned int ms);
  73. };
  74. /*
  75. * Wrapping counters: unsigned long for debugfs_create_ulong(), so 32 bits on
  76. * the target, where i2c_bytes wraps after about a day of continuous traffic
  77. * at 400 kHz. Read them as differences between two samples.
  78. */
  79. struct s1d_stats {
  80. unsigned long writes; /* LCD bus cycles */
  81. unsigned long data_bytes; /* of which display memory */
  82. unsigned long seeks;
  83. unsigned long dir_changes;
  84. unsigned long i2c_msgs;
  85. unsigned long i2c_bytes; /* payload, including command bytes */
  86. };
  87. struct s1d_panel {
  88. struct s1d_config cfg;
  89. unsigned int stride;
  90. unsigned int size;
  91. const struct s1d_ops *ops;
  92. void *ctx;
  93. uint8_t *shadow; /* what the panel holds; cfg-sized */
  94. uint8_t *tx;
  95. size_t tx_cap;
  96. size_t tx_len;
  97. uint8_t p1; /* last control byte queued; WR high between writes */
  98. uint8_t p1_sent; /* last control byte the expander acknowledged */
  99. bool vertical; /* cursor direction currently programmed */
  100. bool backlight; /* S1D_P1_BL level to keep driving */
  101. bool force_full; /* shadow cannot be trusted: rewrite everything */
  102. int err; /* first transport error of the current operation */
  103. struct s1d_stats stats;
  104. };
  105. /* Native framebuffer size for a configuration; 0 if it is not valid. */
  106. unsigned int s1d_frame_size(const struct s1d_config *cfg);
  107. /*
  108. * shadow must hold s1d_frame_size() bytes; tx is the transfer buffer and must
  109. * be DMA-safe in the kernel. tx_cap limits one I2C message (at least 7).
  110. */
  111. int s1d_panel_setup(struct s1d_panel *p, const struct s1d_config *cfg,
  112. const struct s1d_ops *ops, void *ctx,
  113. uint8_t *shadow, uint8_t *tx, size_t tx_cap);
  114. /* Program the expander, reset and configure the controller, clear VRAM. */
  115. int s1d_panel_init(struct s1d_panel *p);
  116. /*
  117. * Bring rows [y0, y1) of the panel up to date with the native frame "next",
  118. * sending only what differs from the shadow. After a transport error the next
  119. * call rewrites the whole frame, so "next" must always be a complete frame.
  120. */
  121. int s1d_panel_update(struct s1d_panel *p, const uint8_t *next,
  122. unsigned int y0, unsigned int y1);
  123. int s1d_panel_display(struct s1d_panel *p, bool on);
  124. /* Backlight pin on the expander. Applied at once if the panel is set up. */
  125. int s1d_panel_backlight(struct s1d_panel *p, bool on);
  126. /*
  127. * Program a raw controller command, for bring-up experiments. CSRDIR_L and
  128. * CSRDIR_U are refused, since the update planner only knows right and down.
  129. */
  130. int s1d_panel_command(struct s1d_panel *p, uint8_t cmd,
  131. const uint8_t *params, size_t nparams);
  132. /*
  133. * Convert XRGB8888 pixels to the native format: white is level 0 (pixel off),
  134. * black the highest level, leftmost pixel in the most significant bits. The
  135. * rectangle is widened to whole bytes; src must cover those pixels.
  136. */
  137. void s1d_convert_xrgb8888(const struct s1d_config *cfg, const void *src,
  138. unsigned int pitch, uint8_t *dst,
  139. unsigned int x0, unsigned int y0,
  140. unsigned int x1, unsigned int y1);
  141. #ifndef __KERNEL__
  142. /* Approximate bus time in microseconds at the given SCL rate. */
  143. uint64_t s1d_stats_bus_us(const struct s1d_stats *st, unsigned int scl_hz);
  144. #endif
  145. #endif /* S1D13700_CORE_H */