s1d13700_core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #ifdef __KERNEL__
  3. #include <linux/errno.h>
  4. #include <linux/string.h>
  5. #else
  6. #include <errno.h>
  7. #include <string.h>
  8. #endif
  9. #include "s1d13700_core.h"
  10. /* Bytes one LCD write can need: A0 settle pair + WR low pair + WR high pair */
  11. #define MAX_WRITE_BYTES 6u
  12. /* ---- Transport --------------------------------------------------------- */
  13. static void tx_reset(struct s1d_panel *p)
  14. {
  15. p->tx[0] = TCA_REG_OUTPUT0;
  16. p->tx_len = 1;
  17. }
  18. static void tx_flush(struct s1d_panel *p)
  19. {
  20. int ret;
  21. if (p->tx_len > 1 && !p->err) {
  22. ret = p->ops->xfer(p->ctx, p->tx, p->tx_len);
  23. if (ret) {
  24. p->err = ret;
  25. p->force_full = true;
  26. } else {
  27. p->p1_sent = p->p1;
  28. p->stats.i2c_msgs++;
  29. p->stats.i2c_bytes += p->tx_len;
  30. }
  31. }
  32. tx_reset(p);
  33. }
  34. static void put_pair(struct s1d_panel *p, uint8_t data, uint8_t ctrl)
  35. {
  36. p->tx[p->tx_len++] = data;
  37. p->tx[p->tx_len++] = ctrl;
  38. p->p1 = ctrl;
  39. }
  40. /*
  41. * One LCD bus cycle. A0 needs setup time before the WR# edge, and both are in
  42. * the same port, so an A0 change gets its own update with WR# still high.
  43. * A write is never split across messages, so WR# is not left low on a bus
  44. * release. The data byte is presented before WR# falls and repeated while it
  45. * is low, because the register pair toggle makes that slot unavoidable.
  46. */
  47. static void bus_write(struct s1d_panel *p, bool a0, uint8_t data)
  48. {
  49. uint8_t ctrl = p->p1 | S1D_P1_WR;
  50. ctrl = a0 ? (ctrl | S1D_P1_A0) : (ctrl & ~S1D_P1_A0);
  51. if (p->tx_len + MAX_WRITE_BYTES > p->tx_cap)
  52. tx_flush(p);
  53. if (ctrl != p->p1)
  54. put_pair(p, data, ctrl);
  55. put_pair(p, data, ctrl & ~S1D_P1_WR);
  56. put_pair(p, data, ctrl);
  57. p->stats.writes++;
  58. }
  59. static void cmd(struct s1d_panel *p, uint8_t c)
  60. {
  61. bus_write(p, true, c);
  62. }
  63. static void param(struct s1d_panel *p, uint8_t v)
  64. {
  65. bus_write(p, false, v);
  66. }
  67. static void param16(struct s1d_panel *p, unsigned int v)
  68. {
  69. param(p, v & 0xff);
  70. param(p, v >> 8);
  71. }
  72. static void seek(struct s1d_panel *p, unsigned int addr)
  73. {
  74. cmd(p, S1D_CMD_CSRW);
  75. param16(p, addr);
  76. cmd(p, S1D_CMD_MWRITE);
  77. p->stats.seeks++;
  78. }
  79. static void set_direction(struct s1d_panel *p, bool vertical)
  80. {
  81. if (p->vertical == vertical)
  82. return;
  83. cmd(p, vertical ? S1D_CMD_CSRDIR_D : S1D_CMD_CSRDIR_R);
  84. p->vertical = vertical;
  85. p->stats.dir_changes++;
  86. }
  87. static int take_error(struct s1d_panel *p)
  88. {
  89. int err = p->err;
  90. /*
  91. * Queued writes that were never sent moved p1 ahead of the pins. Fall
  92. * back to what the expander last accepted, so the next A0 change still
  93. * gets its own update before WR# falls.
  94. */
  95. if (err)
  96. p->p1 = p->p1_sent;
  97. p->err = 0;
  98. return err;
  99. }
  100. /* ---- Setup and init ----------------------------------------------------- */
  101. unsigned int s1d_frame_size(const struct s1d_config *cfg)
  102. {
  103. unsigned int ppb, stride;
  104. if (cfg->bpp != 1 && cfg->bpp != 2 && cfg->bpp != 4)
  105. return 0;
  106. ppb = 8 / cfg->bpp;
  107. if (!cfg->width || !cfg->height || cfg->width % ppb)
  108. return 0;
  109. if (cfg->height > 256)
  110. return 0;
  111. stride = cfg->width / ppb;
  112. /* C/R register is 8 bits and capped at 253; the whole frame must fit */
  113. if (stride - 1 > 253 || stride * cfg->height > S1D_VRAM_SIZE)
  114. return 0;
  115. /*
  116. * The register holds [TC/R] - 1. Technical Manual: [TC/R] >= [C/R] + 4
  117. * (the Hardware Functional Specification says + 2; take the stricter).
  118. */
  119. if (cfg->tcr > 255 || cfg->tcr < stride + 3)
  120. return 0;
  121. return stride * cfg->height;
  122. }
  123. int s1d_panel_setup(struct s1d_panel *p, const struct s1d_config *cfg,
  124. const struct s1d_ops *ops, void *ctx,
  125. uint8_t *shadow, uint8_t *tx, size_t tx_cap)
  126. {
  127. unsigned int size = s1d_frame_size(cfg);
  128. if (!size || tx_cap < MAX_WRITE_BYTES + 1)
  129. return -EINVAL;
  130. memset(p, 0, sizeof(*p));
  131. p->cfg = *cfg;
  132. p->size = size;
  133. p->stride = size / cfg->height;
  134. p->ops = ops;
  135. p->ctx = ctx;
  136. p->shadow = shadow;
  137. p->tx = tx;
  138. p->tx_cap = tx_cap;
  139. p->force_full = true;
  140. tx_reset(p);
  141. return 0;
  142. }
  143. static void write_regs(struct s1d_panel *p, uint8_t reg, uint8_t v0, uint8_t v1)
  144. {
  145. p->tx[0] = reg;
  146. p->tx[1] = v0;
  147. p->tx[2] = v1;
  148. p->tx_len = 3;
  149. tx_flush(p);
  150. }
  151. static void clear_vram(struct s1d_panel *p)
  152. {
  153. unsigned int i;
  154. seek(p, 0);
  155. for (i = 0; i < p->size; i++)
  156. param(p, 0);
  157. memset(p->shadow, 0, p->size);
  158. }
  159. int s1d_panel_init(struct s1d_panel *p)
  160. {
  161. const struct s1d_config *c = &p->cfg;
  162. unsigned int bpp_code = c->bpp == 4 ? 2 : c->bpp == 2 ? 1 : 0;
  163. p->err = 0;
  164. p->vertical = false;
  165. /*
  166. * Output latches first, then direction, so no control line glitches.
  167. * RES# stays low; WR#, RD#, A0 and CS# idle high.
  168. */
  169. p->p1 = S1D_P1_WR | S1D_P1_RD | S1D_P1_A0 | S1D_P1_CS |
  170. (p->backlight ? S1D_P1_BL : 0);
  171. write_regs(p, TCA_REG_OUTPUT0, 0x00, p->p1);
  172. write_regs(p, TCA_REG_POLARITY0, 0x00, 0x00);
  173. write_regs(p, TCA_REG_CONFIG0, 0x00, S1D_P1_CONFIG);
  174. p->ops->delay_ms(p->ctx, 20);
  175. /* out of reset and selected for good: WR# frames every cycle */
  176. p->p1 |= S1D_P1_RES;
  177. write_regs(p, TCA_REG_OUTPUT0, 0x00, p->p1);
  178. p->p1 &= ~S1D_P1_CS;
  179. write_regs(p, TCA_REG_OUTPUT0, 0x00, p->p1);
  180. p->ops->delay_ms(p->ctx, 20);
  181. if (p->err)
  182. return take_error(p);
  183. /*
  184. * Command + P1 releases power save; a crystal oscillator then needs
  185. * at least 3 ms before SYSTEM SET is sent again in full. Harmless
  186. * with an external clock on CLKI.
  187. */
  188. cmd(p, S1D_CMD_SYSTEM_SET);
  189. param(p, 0x10 | 1 << 5);
  190. tx_flush(p);
  191. p->ops->delay_ms(p->ctx, 10);
  192. cmd(p, S1D_CMD_SYSTEM_SET);
  193. param(p, 0x10 | 1 << 5); /* single panel, no origin offset */
  194. param(p, 0x80 | 7); /* FX = 7: required above 1 bpp */
  195. param(p, 7); /* FY */
  196. param(p, p->stride - 1); /* C/R */
  197. param(p, c->tcr); /* TC/R */
  198. param(p, c->height - 1); /* L/F */
  199. param16(p, p->stride); /* AP */
  200. /*
  201. * One graphics block. The second layer is blanked below but still gets
  202. * a sane address. SL uses the same lines-1 value as L/F.
  203. */
  204. cmd(p, S1D_CMD_SCROLL);
  205. param16(p, 0); /* SAD1 */
  206. param(p, c->height - 1); /* SL1 */
  207. param16(p, p->size); /* SAD2 */
  208. param(p, c->height - 1); /* SL2 */
  209. param16(p, 0); /* SAD3 */
  210. param16(p, 0); /* SAD4 */
  211. cmd(p, S1D_CMD_CSRFORM);
  212. param(p, 0x04);
  213. param(p, 0x80 | 7); /* CM must be 1 in graphics mode */
  214. cmd(p, S1D_CMD_CGRAM_ADR);
  215. param16(p, 0x7000);
  216. cmd(p, S1D_CMD_CSRDIR_R);
  217. cmd(p, S1D_CMD_HDOT_SCR);
  218. param(p, 0); /* must be 0 above 1 bpp */
  219. cmd(p, S1D_CMD_OVLAY);
  220. param(p, 0x0c); /* both layers graphics, OR */
  221. cmd(p, S1D_CMD_GRAYSCALE);
  222. param(p, bpp_code);
  223. /* clear before showing, so no power-on garbage is ever visible */
  224. clear_vram(p);
  225. cmd(p, S1D_CMD_DISP_ON);
  226. param(p, 0x04); /* SAD1 on, others and cursor off */
  227. tx_flush(p);
  228. if (!p->err)
  229. p->force_full = false;
  230. return take_error(p);
  231. }
  232. int s1d_panel_display(struct s1d_panel *p, bool on)
  233. {
  234. cmd(p, on ? S1D_CMD_DISP_ON : S1D_CMD_DISP_OFF);
  235. param(p, 0x04);
  236. tx_flush(p);
  237. return take_error(p);
  238. }
  239. int s1d_panel_backlight(struct s1d_panel *p, bool on)
  240. {
  241. p->backlight = on;
  242. /* nothing accepted yet: the pins are still inputs, init sets the level */
  243. if (!p->p1_sent)
  244. return 0;
  245. p->p1 = on ? (p->p1 | S1D_P1_BL) : (p->p1 & ~S1D_P1_BL);
  246. /* WR# is high here, so a lone P0/P1 pair is no bus cycle */
  247. write_regs(p, TCA_REG_OUTPUT0, 0x00, p->p1);
  248. return take_error(p);
  249. }
  250. int s1d_panel_command(struct s1d_panel *p, uint8_t c,
  251. const uint8_t *params, size_t nparams)
  252. {
  253. size_t i;
  254. /* p->vertical only models right and down */
  255. if (c == S1D_CMD_CSRDIR_L || c == S1D_CMD_CSRDIR_U)
  256. return -EINVAL;
  257. if (c == S1D_CMD_CSRDIR_R || c == S1D_CMD_CSRDIR_D)
  258. p->vertical = c == S1D_CMD_CSRDIR_D;
  259. cmd(p, c);
  260. for (i = 0; i < nparams; i++)
  261. param(p, params[i]);
  262. tx_flush(p);
  263. return take_error(p);
  264. }
  265. /* ---- Update planner ----------------------------------------------------- */
  266. static bool differs(const struct s1d_panel *p, const uint8_t *next, unsigned int a)
  267. {
  268. return p->force_full || next[a] != p->shadow[a];
  269. }
  270. /*
  271. * Walk a line of display memory (horizontally or down one column). Changed
  272. * bytes are grouped into runs; an unchanged gap no longer than a seek is
  273. * rewritten rather than seeked over.
  274. */
  275. static unsigned long scan_cost(const struct s1d_panel *p, const uint8_t *next,
  276. unsigned int start, unsigned int step,
  277. unsigned int count)
  278. {
  279. unsigned long cost = 0;
  280. unsigned int i, last = 0;
  281. bool open = false;
  282. for (i = 0; i < count; i++) {
  283. if (!differs(p, next, start + i * step))
  284. continue;
  285. if (open && i - last - 1 <= S1D_SEEK_COST)
  286. cost += i - last;
  287. else
  288. cost += S1D_SEEK_COST + 1;
  289. last = i;
  290. open = true;
  291. }
  292. return cost;
  293. }
  294. static void scan_emit(struct s1d_panel *p, const uint8_t *next,
  295. unsigned int start, unsigned int step, unsigned int count)
  296. {
  297. unsigned int i = 0;
  298. /* after a bus error nothing more is sent, and force_full is set */
  299. while (i < count && !p->err) {
  300. unsigned int run_end, gap_scan, k;
  301. if (!differs(p, next, start + i * step)) {
  302. i++;
  303. continue;
  304. }
  305. /* extend the run while the next change is within reach */
  306. run_end = i;
  307. gap_scan = i + 1;
  308. while (gap_scan < count && gap_scan - run_end - 1 <= S1D_SEEK_COST) {
  309. if (differs(p, next, start + gap_scan * step))
  310. run_end = gap_scan;
  311. gap_scan++;
  312. }
  313. seek(p, start + i * step);
  314. for (k = i; k <= run_end && !p->err; k++) {
  315. unsigned int a = start + k * step;
  316. param(p, next[a]);
  317. p->shadow[a] = next[a];
  318. p->stats.data_bytes++;
  319. }
  320. i = run_end + 1;
  321. }
  322. }
  323. /* Rows [y0, y1) all contain changes: pick the cheaper write direction. */
  324. static void update_band(struct s1d_panel *p, const uint8_t *next,
  325. unsigned int y0, unsigned int y1)
  326. {
  327. unsigned int rows = y1 - y0, x;
  328. unsigned long horiz, vert = 0;
  329. /* memory is linear, so full-width runs continue across row ends */
  330. horiz = scan_cost(p, next, y0 * p->stride, 1, rows * p->stride);
  331. for (x = 0; x < p->stride; x++)
  332. vert += scan_cost(p, next, y0 * p->stride + x, p->stride, rows);
  333. horiz += p->vertical ? S1D_DIR_COST : 0;
  334. vert += p->vertical ? 0 : S1D_DIR_COST;
  335. if (horiz <= vert) {
  336. set_direction(p, false);
  337. scan_emit(p, next, y0 * p->stride, 1, rows * p->stride);
  338. } else {
  339. set_direction(p, true);
  340. for (x = 0; x < p->stride; x++)
  341. scan_emit(p, next, y0 * p->stride + x, p->stride, rows);
  342. }
  343. }
  344. static bool row_differs(const struct s1d_panel *p, const uint8_t *next, unsigned int y)
  345. {
  346. return p->force_full ||
  347. memcmp(next + y * p->stride, p->shadow + y * p->stride, p->stride);
  348. }
  349. int s1d_panel_update(struct s1d_panel *p, const uint8_t *next,
  350. unsigned int y0, unsigned int y1)
  351. {
  352. bool full = p->force_full;
  353. unsigned int y;
  354. if (full) {
  355. /* the hardware state is in doubt, including the cursor direction */
  356. y0 = 0;
  357. y1 = p->cfg.height;
  358. cmd(p, p->vertical ? S1D_CMD_CSRDIR_D : S1D_CMD_CSRDIR_R);
  359. }
  360. if (y1 > p->cfg.height)
  361. y1 = p->cfg.height;
  362. y = y0;
  363. while (y < y1 && !p->err) {
  364. unsigned int band_end;
  365. if (!row_differs(p, next, y)) {
  366. y++;
  367. continue;
  368. }
  369. band_end = y + 1;
  370. while (band_end < y1 && row_differs(p, next, band_end))
  371. band_end++;
  372. update_band(p, next, y, band_end);
  373. y = band_end;
  374. }
  375. tx_flush(p);
  376. if (full && !p->err)
  377. p->force_full = false;
  378. return take_error(p);
  379. }
  380. /* ---- Pixel conversion --------------------------------------------------- */
  381. void s1d_convert_xrgb8888(const struct s1d_config *cfg, const void *src,
  382. unsigned int pitch, uint8_t *dst,
  383. unsigned int x0, unsigned int y0,
  384. unsigned int x1, unsigned int y1)
  385. {
  386. unsigned int bpp = cfg->bpp, ppb = 8 / bpp;
  387. unsigned int stride = cfg->width / ppb;
  388. unsigned int x, y;
  389. x0 -= x0 % ppb;
  390. x1 = (x1 + ppb - 1) / ppb * ppb;
  391. if (x1 > cfg->width)
  392. x1 = cfg->width;
  393. if (y1 > cfg->height)
  394. y1 = cfg->height;
  395. for (y = y0; y < y1; y++) {
  396. const uint32_t *line = (const uint32_t *)
  397. ((const uint8_t *)src + (size_t)y * pitch);
  398. uint8_t *out = dst + y * stride + x0 / ppb;
  399. for (x = x0; x < x1; x += ppb) {
  400. uint8_t byte = 0;
  401. unsigned int i;
  402. for (i = 0; i < ppb; i++) {
  403. uint32_t c = line[x + i];
  404. unsigned int luma = (((c >> 16) & 0xff) * 77 +
  405. ((c >> 8) & 0xff) * 150 +
  406. (c & 0xff) * 29) >> 8;
  407. byte = (uint8_t)(byte << bpp) |
  408. ((255 - luma) >> (8 - bpp));
  409. }
  410. *out++ = byte;
  411. }
  412. }
  413. }
  414. #ifndef __KERNEL__ /* the driver has no use for it, so no div_u64() needed */
  415. uint64_t s1d_stats_bus_us(const struct s1d_stats *st, unsigned int scl_hz)
  416. {
  417. /* 9 clocks per byte, plus address byte and START/STOP per message */
  418. uint64_t clocks = ((uint64_t)st->i2c_bytes + st->i2c_msgs) * 9 +
  419. (uint64_t)st->i2c_msgs * 2;
  420. return scl_hz ? clocks * 1000000u / scl_hz : 0;
  421. }
  422. #endif