s1d13700_core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. int ret;
  247. /* nothing accepted yet: the pins are still inputs, init sets the level */
  248. if (!p->p1_sent) {
  249. p->backlight = on;
  250. return 0;
  251. }
  252. p->p1 = on ? (p->p1 | S1D_P1_BL) : (p->p1 & ~S1D_P1_BL);
  253. /* WR# is high here, so a lone P0/P1 pair is no bus cycle */
  254. write_regs(p, TCA_REG_OUTPUT0, 0x00, p->p1);
  255. /*
  256. * On failure take_error() puts p1 back to the old level, which later
  257. * writes keep driving; leave backlight saying so, so callers that
  258. * compare against it try again.
  259. */
  260. ret = take_error(p);
  261. if (!ret)
  262. p->backlight = on;
  263. return ret;
  264. }
  265. int s1d_panel_command(struct s1d_panel *p, uint8_t c,
  266. const uint8_t *params, size_t nparams)
  267. {
  268. size_t i;
  269. /* p->vertical only models right and down */
  270. if (c == S1D_CMD_CSRDIR_L || c == S1D_CMD_CSRDIR_U)
  271. return -EINVAL;
  272. if (c == S1D_CMD_CSRDIR_R || c == S1D_CMD_CSRDIR_D)
  273. p->vertical = c == S1D_CMD_CSRDIR_D;
  274. cmd(p, c);
  275. for (i = 0; i < nparams; i++)
  276. param(p, params[i]);
  277. tx_flush(p);
  278. return take_error(p);
  279. }
  280. /* ---- Update planner ----------------------------------------------------- */
  281. static bool differs(const struct s1d_panel *p, const uint8_t *next, unsigned int a)
  282. {
  283. return p->force_full || next[a] != p->shadow[a];
  284. }
  285. /*
  286. * Walk a line of display memory (horizontally or down one column). Changed
  287. * bytes are grouped into runs; an unchanged gap no longer than a seek is
  288. * rewritten rather than seeked over.
  289. */
  290. static unsigned long scan_cost(const struct s1d_panel *p, const uint8_t *next,
  291. unsigned int start, unsigned int step,
  292. unsigned int count)
  293. {
  294. unsigned long cost = 0;
  295. unsigned int i, last = 0;
  296. bool open = false;
  297. for (i = 0; i < count; i++) {
  298. if (!differs(p, next, start + i * step))
  299. continue;
  300. if (open && i - last - 1 <= S1D_SEEK_COST)
  301. cost += i - last;
  302. else
  303. cost += S1D_SEEK_COST + 1;
  304. last = i;
  305. open = true;
  306. }
  307. return cost;
  308. }
  309. static void scan_emit(struct s1d_panel *p, const uint8_t *next,
  310. unsigned int start, unsigned int step, unsigned int count)
  311. {
  312. unsigned int i = 0;
  313. /* after a bus error nothing more is sent, and force_full is set */
  314. while (i < count && !p->err) {
  315. unsigned int run_end, gap_scan, k;
  316. if (!differs(p, next, start + i * step)) {
  317. i++;
  318. continue;
  319. }
  320. /* extend the run while the next change is within reach */
  321. run_end = i;
  322. gap_scan = i + 1;
  323. while (gap_scan < count && gap_scan - run_end - 1 <= S1D_SEEK_COST) {
  324. if (differs(p, next, start + gap_scan * step))
  325. run_end = gap_scan;
  326. gap_scan++;
  327. }
  328. seek(p, start + i * step);
  329. for (k = i; k <= run_end && !p->err; k++) {
  330. unsigned int a = start + k * step;
  331. param(p, next[a]);
  332. p->shadow[a] = next[a];
  333. p->stats.data_bytes++;
  334. }
  335. i = run_end + 1;
  336. }
  337. }
  338. /* Rows [y0, y1) all contain changes: pick the cheaper write direction. */
  339. static void update_band(struct s1d_panel *p, const uint8_t *next,
  340. unsigned int y0, unsigned int y1)
  341. {
  342. unsigned int rows = y1 - y0, x;
  343. unsigned long horiz, vert = 0;
  344. /* memory is linear, so full-width runs continue across row ends */
  345. horiz = scan_cost(p, next, y0 * p->stride, 1, rows * p->stride);
  346. for (x = 0; x < p->stride; x++)
  347. vert += scan_cost(p, next, y0 * p->stride + x, p->stride, rows);
  348. horiz += p->vertical ? S1D_DIR_COST : 0;
  349. vert += p->vertical ? 0 : S1D_DIR_COST;
  350. if (horiz <= vert) {
  351. set_direction(p, false);
  352. scan_emit(p, next, y0 * p->stride, 1, rows * p->stride);
  353. } else {
  354. set_direction(p, true);
  355. for (x = 0; x < p->stride; x++)
  356. scan_emit(p, next, y0 * p->stride + x, p->stride, rows);
  357. }
  358. }
  359. static bool row_differs(const struct s1d_panel *p, const uint8_t *next, unsigned int y)
  360. {
  361. return p->force_full ||
  362. memcmp(next + y * p->stride, p->shadow + y * p->stride, p->stride);
  363. }
  364. int s1d_panel_update(struct s1d_panel *p, const uint8_t *next,
  365. unsigned int y0, unsigned int y1)
  366. {
  367. bool full = p->force_full;
  368. unsigned long writes = p->stats.writes;
  369. unsigned int y;
  370. if (full) {
  371. /* the hardware state is in doubt, including the cursor direction */
  372. y0 = 0;
  373. y1 = p->cfg.height;
  374. cmd(p, p->vertical ? S1D_CMD_CSRDIR_D : S1D_CMD_CSRDIR_R);
  375. }
  376. if (y1 > p->cfg.height)
  377. y1 = p->cfg.height;
  378. y = y0;
  379. while (y < y1 && !p->err) {
  380. unsigned int band_end;
  381. if (!row_differs(p, next, y)) {
  382. y++;
  383. continue;
  384. }
  385. band_end = y + 1;
  386. while (band_end < y1 && row_differs(p, next, band_end))
  387. band_end++;
  388. update_band(p, next, y, band_end);
  389. y = band_end;
  390. }
  391. /*
  392. * A memory write turns the cursor on even when the attributes say off
  393. * (HW Functional Spec, REG[0Ah] note), leaving a block at the last
  394. * write position. Re-sending the attributes hides it again.
  395. */
  396. if (p->stats.writes != writes && p->display_on) {
  397. cmd(p, S1D_CMD_DISP_ON);
  398. param(p, DISP_ATTR);
  399. }
  400. tx_flush(p);
  401. if (full && !p->err)
  402. p->force_full = false;
  403. return take_error(p);
  404. }
  405. /* ---- Pixel conversion --------------------------------------------------- */
  406. void s1d_convert_xrgb8888(const struct s1d_config *cfg, const void *src,
  407. unsigned int pitch, uint8_t *dst,
  408. unsigned int x0, unsigned int y0,
  409. unsigned int x1, unsigned int y1)
  410. {
  411. unsigned int bpp = cfg->bpp, ppb = 8 / bpp;
  412. unsigned int stride = cfg->width / ppb;
  413. unsigned int x, y;
  414. x0 -= x0 % ppb;
  415. x1 = (x1 + ppb - 1) / ppb * ppb;
  416. if (x1 > cfg->width)
  417. x1 = cfg->width;
  418. if (y1 > cfg->height)
  419. y1 = cfg->height;
  420. for (y = y0; y < y1; y++) {
  421. const uint32_t *line = (const uint32_t *)
  422. ((const uint8_t *)src + (size_t)y * pitch);
  423. uint8_t *out = dst + y * stride + x0 / ppb;
  424. for (x = x0; x < x1; x += ppb) {
  425. uint8_t byte = 0;
  426. unsigned int i;
  427. for (i = 0; i < ppb; i++) {
  428. uint32_t c = line[x + i];
  429. unsigned int luma = (((c >> 16) & 0xff) * 77 +
  430. ((c >> 8) & 0xff) * 150 +
  431. (c & 0xff) * 29) >> 8;
  432. byte = (uint8_t)(byte << bpp) |
  433. ((255 - luma) >> (8 - bpp));
  434. }
  435. *out++ = byte;
  436. }
  437. }
  438. }
  439. #ifndef __KERNEL__ /* the driver has no use for it, so no div_u64() needed */
  440. uint64_t s1d_stats_bus_us(const struct s1d_stats *st, unsigned int scl_hz)
  441. {
  442. /* 9 clocks per byte, plus address byte and START/STOP per message */
  443. uint64_t clocks = ((uint64_t)st->i2c_bytes + st->i2c_msgs) * 9 +
  444. (uint64_t)st->i2c_msgs * 2;
  445. return scl_hz ? clocks * 1000000u / scl_hz : 0;
  446. }
  447. #endif