s1d13700_core.c 13 KB

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