| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517 |
- // SPDX-License-Identifier: GPL-2.0-only
- #ifdef __KERNEL__
- #include <linux/errno.h>
- #include <linux/string.h>
- #else
- #include <errno.h>
- #include <string.h>
- #endif
- #include "s1d13700_core.h"
- /* Bytes one LCD write can need: A0 settle pair + WR low pair + WR high pair */
- #define MAX_WRITE_BYTES 6u
- /* DISP ON/OFF attributes: SAD1 on, other blocks and the cursor off */
- #define DISP_ATTR 0x04
- /* ---- Transport --------------------------------------------------------- */
- static void tx_reset(struct s1d_panel *p)
- {
- p->tx[0] = TCA_REG_OUTPUT0;
- p->tx_len = 1;
- }
- static void tx_flush(struct s1d_panel *p)
- {
- int ret;
- if (p->tx_len > 1 && !p->err) {
- ret = p->ops->xfer(p->ctx, p->tx, p->tx_len);
- if (ret) {
- p->err = ret;
- p->force_full = true;
- } else {
- p->p1_sent = p->p1;
- p->stats.i2c_msgs++;
- p->stats.i2c_bytes += p->tx_len;
- }
- }
- tx_reset(p);
- }
- static void put_pair(struct s1d_panel *p, uint8_t data, uint8_t ctrl)
- {
- p->tx[p->tx_len++] = data;
- p->tx[p->tx_len++] = ctrl;
- p->p1 = ctrl;
- }
- /*
- * One LCD bus cycle. A0 needs setup time before the WR# edge, and both are in
- * the same port, so an A0 change gets its own update with WR# still high.
- * A write is never split across messages, so WR# is not left low on a bus
- * release. The data byte is presented before WR# falls and repeated while it
- * is low, because the register pair toggle makes that slot unavoidable.
- */
- static void bus_write(struct s1d_panel *p, bool a0, uint8_t data)
- {
- uint8_t ctrl = p->p1 | S1D_P1_WR;
- ctrl = a0 ? (ctrl | S1D_P1_A0) : (ctrl & ~S1D_P1_A0);
- if (p->tx_len + MAX_WRITE_BYTES > p->tx_cap)
- tx_flush(p);
- if (ctrl != p->p1)
- put_pair(p, data, ctrl);
- put_pair(p, data, ctrl & ~S1D_P1_WR);
- put_pair(p, data, ctrl);
- p->stats.writes++;
- }
- static void cmd(struct s1d_panel *p, uint8_t c)
- {
- bus_write(p, true, c);
- }
- static void param(struct s1d_panel *p, uint8_t v)
- {
- bus_write(p, false, v);
- }
- static void param16(struct s1d_panel *p, unsigned int v)
- {
- param(p, v & 0xff);
- param(p, v >> 8);
- }
- static void seek(struct s1d_panel *p, unsigned int addr)
- {
- cmd(p, S1D_CMD_CSRW);
- param16(p, addr);
- cmd(p, S1D_CMD_MWRITE);
- p->stats.seeks++;
- }
- static void set_direction(struct s1d_panel *p, bool vertical)
- {
- if (p->vertical == vertical)
- return;
- cmd(p, vertical ? S1D_CMD_CSRDIR_D : S1D_CMD_CSRDIR_R);
- p->vertical = vertical;
- p->stats.dir_changes++;
- }
- static int take_error(struct s1d_panel *p)
- {
- int err = p->err;
- /*
- * Queued writes that were never sent moved p1 ahead of the pins. Fall
- * back to what the expander last accepted, so the next A0 change still
- * gets its own update before WR# falls.
- */
- if (err)
- p->p1 = p->p1_sent;
- p->err = 0;
- return err;
- }
- /* ---- Setup and init ----------------------------------------------------- */
- unsigned int s1d_frame_size(const struct s1d_config *cfg)
- {
- unsigned int ppb, stride;
- if (cfg->bpp != 1 && cfg->bpp != 2 && cfg->bpp != 4)
- return 0;
- ppb = 8 / cfg->bpp;
- if (!cfg->width || !cfg->height || cfg->width % ppb)
- return 0;
- if (cfg->height > 256)
- return 0;
- stride = cfg->width / ppb;
- /* C/R register is 8 bits and capped at 253; the whole frame must fit */
- if (stride - 1 > 253 || stride * cfg->height > S1D_VRAM_SIZE)
- return 0;
- /*
- * The register holds [TC/R] - 1. Technical Manual: [TC/R] >= [C/R] + 4
- * (the Hardware Functional Specification says + 2; take the stricter).
- */
- if (cfg->tcr > 255 || cfg->tcr < stride + 3)
- return 0;
- return stride * cfg->height;
- }
- int s1d_panel_setup(struct s1d_panel *p, const struct s1d_config *cfg,
- const struct s1d_ops *ops, void *ctx,
- uint8_t *shadow, uint8_t *tx, size_t tx_cap)
- {
- unsigned int size = s1d_frame_size(cfg);
- if (!size || tx_cap < MAX_WRITE_BYTES + 1)
- return -EINVAL;
- memset(p, 0, sizeof(*p));
- p->cfg = *cfg;
- p->size = size;
- p->stride = size / cfg->height;
- p->ops = ops;
- p->ctx = ctx;
- p->shadow = shadow;
- p->tx = tx;
- p->tx_cap = tx_cap;
- p->force_full = true;
- tx_reset(p);
- return 0;
- }
- static void write_regs(struct s1d_panel *p, uint8_t reg, uint8_t v0, uint8_t v1)
- {
- p->tx[0] = reg;
- p->tx[1] = v0;
- p->tx[2] = v1;
- p->tx_len = 3;
- tx_flush(p);
- }
- static void clear_vram(struct s1d_panel *p)
- {
- unsigned int i;
- seek(p, 0);
- for (i = 0; i < p->size; i++)
- param(p, 0);
- memset(p->shadow, 0, p->size);
- }
- int s1d_panel_init(struct s1d_panel *p)
- {
- const struct s1d_config *c = &p->cfg;
- unsigned int bpp_code = c->bpp == 4 ? 2 : c->bpp == 2 ? 1 : 0;
- p->err = 0;
- p->vertical = false;
- /*
- * Output latches first, then direction, so no control line glitches.
- * RES# stays low; WR#, RD#, A0 and CS# idle high.
- */
- p->p1 = S1D_P1_WR | S1D_P1_RD | S1D_P1_A0 | S1D_P1_CS |
- (p->backlight ? S1D_P1_BL : 0);
- write_regs(p, TCA_REG_OUTPUT0, 0x00, p->p1);
- write_regs(p, TCA_REG_POLARITY0, 0x00, 0x00);
- write_regs(p, TCA_REG_CONFIG0, 0x00, S1D_P1_CONFIG);
- p->ops->delay_ms(p->ctx, 20);
- /* out of reset and selected for good: WR# frames every cycle */
- p->p1 |= S1D_P1_RES;
- write_regs(p, TCA_REG_OUTPUT0, 0x00, p->p1);
- p->p1 &= ~S1D_P1_CS;
- write_regs(p, TCA_REG_OUTPUT0, 0x00, p->p1);
- p->ops->delay_ms(p->ctx, 20);
- if (p->err)
- return take_error(p);
- /*
- * Command + P1 releases power save; a crystal oscillator then needs
- * at least 3 ms before SYSTEM SET is sent again in full. Harmless
- * with an external clock on CLKI.
- */
- cmd(p, S1D_CMD_SYSTEM_SET);
- param(p, 0x10 | 1 << 5);
- tx_flush(p);
- p->ops->delay_ms(p->ctx, 10);
- cmd(p, S1D_CMD_SYSTEM_SET);
- param(p, 0x10 | 1 << 5); /* single panel, no origin offset */
- param(p, 0x80 | 7); /* FX = 7: required above 1 bpp */
- param(p, 7); /* FY */
- param(p, p->stride - 1); /* C/R */
- param(p, c->tcr); /* TC/R */
- param(p, c->height - 1); /* L/F */
- param16(p, p->stride); /* AP */
- /*
- * One graphics block. The second layer is blanked below but still gets
- * a sane address. SL uses the same lines-1 value as L/F.
- */
- cmd(p, S1D_CMD_SCROLL);
- param16(p, 0); /* SAD1 */
- param(p, c->height - 1); /* SL1 */
- param16(p, p->size); /* SAD2 */
- param(p, c->height - 1); /* SL2 */
- param16(p, 0); /* SAD3 */
- param16(p, 0); /* SAD4 */
- cmd(p, S1D_CMD_CSRFORM);
- param(p, 0x04);
- param(p, 0x80 | 7); /* CM must be 1 in graphics mode */
- cmd(p, S1D_CMD_CGRAM_ADR);
- param16(p, 0x7000);
- cmd(p, S1D_CMD_CSRDIR_R);
- cmd(p, S1D_CMD_HDOT_SCR);
- param(p, 0); /* must be 0 above 1 bpp */
- cmd(p, S1D_CMD_OVLAY);
- param(p, 0x0c); /* both layers graphics, OR */
- cmd(p, S1D_CMD_GRAYSCALE);
- param(p, bpp_code);
- /* clear before showing, so no power-on garbage is ever visible */
- clear_vram(p);
- cmd(p, S1D_CMD_DISP_ON);
- param(p, DISP_ATTR);
- tx_flush(p);
- if (!p->err) {
- p->force_full = false;
- p->display_on = true;
- }
- return take_error(p);
- }
- int s1d_panel_display(struct s1d_panel *p, bool on)
- {
- cmd(p, on ? S1D_CMD_DISP_ON : S1D_CMD_DISP_OFF);
- param(p, DISP_ATTR);
- tx_flush(p);
- p->display_on = on;
- return take_error(p);
- }
- int s1d_panel_backlight(struct s1d_panel *p, bool on)
- {
- p->backlight = on;
- /* nothing accepted yet: the pins are still inputs, init sets the level */
- if (!p->p1_sent)
- return 0;
- p->p1 = on ? (p->p1 | S1D_P1_BL) : (p->p1 & ~S1D_P1_BL);
- /* WR# is high here, so a lone P0/P1 pair is no bus cycle */
- write_regs(p, TCA_REG_OUTPUT0, 0x00, p->p1);
- return take_error(p);
- }
- int s1d_panel_command(struct s1d_panel *p, uint8_t c,
- const uint8_t *params, size_t nparams)
- {
- size_t i;
- /* p->vertical only models right and down */
- if (c == S1D_CMD_CSRDIR_L || c == S1D_CMD_CSRDIR_U)
- return -EINVAL;
- if (c == S1D_CMD_CSRDIR_R || c == S1D_CMD_CSRDIR_D)
- p->vertical = c == S1D_CMD_CSRDIR_D;
- cmd(p, c);
- for (i = 0; i < nparams; i++)
- param(p, params[i]);
- tx_flush(p);
- return take_error(p);
- }
- /* ---- Update planner ----------------------------------------------------- */
- static bool differs(const struct s1d_panel *p, const uint8_t *next, unsigned int a)
- {
- return p->force_full || next[a] != p->shadow[a];
- }
- /*
- * Walk a line of display memory (horizontally or down one column). Changed
- * bytes are grouped into runs; an unchanged gap no longer than a seek is
- * rewritten rather than seeked over.
- */
- static unsigned long scan_cost(const struct s1d_panel *p, const uint8_t *next,
- unsigned int start, unsigned int step,
- unsigned int count)
- {
- unsigned long cost = 0;
- unsigned int i, last = 0;
- bool open = false;
- for (i = 0; i < count; i++) {
- if (!differs(p, next, start + i * step))
- continue;
- if (open && i - last - 1 <= S1D_SEEK_COST)
- cost += i - last;
- else
- cost += S1D_SEEK_COST + 1;
- last = i;
- open = true;
- }
- return cost;
- }
- static void scan_emit(struct s1d_panel *p, const uint8_t *next,
- unsigned int start, unsigned int step, unsigned int count)
- {
- unsigned int i = 0;
- /* after a bus error nothing more is sent, and force_full is set */
- while (i < count && !p->err) {
- unsigned int run_end, gap_scan, k;
- if (!differs(p, next, start + i * step)) {
- i++;
- continue;
- }
- /* extend the run while the next change is within reach */
- run_end = i;
- gap_scan = i + 1;
- while (gap_scan < count && gap_scan - run_end - 1 <= S1D_SEEK_COST) {
- if (differs(p, next, start + gap_scan * step))
- run_end = gap_scan;
- gap_scan++;
- }
- seek(p, start + i * step);
- for (k = i; k <= run_end && !p->err; k++) {
- unsigned int a = start + k * step;
- param(p, next[a]);
- p->shadow[a] = next[a];
- p->stats.data_bytes++;
- }
- i = run_end + 1;
- }
- }
- /* Rows [y0, y1) all contain changes: pick the cheaper write direction. */
- static void update_band(struct s1d_panel *p, const uint8_t *next,
- unsigned int y0, unsigned int y1)
- {
- unsigned int rows = y1 - y0, x;
- unsigned long horiz, vert = 0;
- /* memory is linear, so full-width runs continue across row ends */
- horiz = scan_cost(p, next, y0 * p->stride, 1, rows * p->stride);
- for (x = 0; x < p->stride; x++)
- vert += scan_cost(p, next, y0 * p->stride + x, p->stride, rows);
- horiz += p->vertical ? S1D_DIR_COST : 0;
- vert += p->vertical ? 0 : S1D_DIR_COST;
- if (horiz <= vert) {
- set_direction(p, false);
- scan_emit(p, next, y0 * p->stride, 1, rows * p->stride);
- } else {
- set_direction(p, true);
- for (x = 0; x < p->stride; x++)
- scan_emit(p, next, y0 * p->stride + x, p->stride, rows);
- }
- }
- static bool row_differs(const struct s1d_panel *p, const uint8_t *next, unsigned int y)
- {
- return p->force_full ||
- memcmp(next + y * p->stride, p->shadow + y * p->stride, p->stride);
- }
- int s1d_panel_update(struct s1d_panel *p, const uint8_t *next,
- unsigned int y0, unsigned int y1)
- {
- bool full = p->force_full;
- unsigned long writes = p->stats.writes;
- unsigned int y;
- if (full) {
- /* the hardware state is in doubt, including the cursor direction */
- y0 = 0;
- y1 = p->cfg.height;
- cmd(p, p->vertical ? S1D_CMD_CSRDIR_D : S1D_CMD_CSRDIR_R);
- }
- if (y1 > p->cfg.height)
- y1 = p->cfg.height;
- y = y0;
- while (y < y1 && !p->err) {
- unsigned int band_end;
- if (!row_differs(p, next, y)) {
- y++;
- continue;
- }
- band_end = y + 1;
- while (band_end < y1 && row_differs(p, next, band_end))
- band_end++;
- update_band(p, next, y, band_end);
- y = band_end;
- }
- /*
- * A memory write turns the cursor on even when the attributes say off
- * (HW Functional Spec, REG[0Ah] note), leaving a block at the last
- * write position. Re-sending the attributes hides it again.
- */
- if (p->stats.writes != writes && p->display_on) {
- cmd(p, S1D_CMD_DISP_ON);
- param(p, DISP_ATTR);
- }
- tx_flush(p);
- if (full && !p->err)
- p->force_full = false;
- return take_error(p);
- }
- /* ---- Pixel conversion --------------------------------------------------- */
- void s1d_convert_xrgb8888(const struct s1d_config *cfg, const void *src,
- unsigned int pitch, uint8_t *dst,
- unsigned int x0, unsigned int y0,
- unsigned int x1, unsigned int y1)
- {
- unsigned int bpp = cfg->bpp, ppb = 8 / bpp;
- unsigned int stride = cfg->width / ppb;
- unsigned int x, y;
- x0 -= x0 % ppb;
- x1 = (x1 + ppb - 1) / ppb * ppb;
- if (x1 > cfg->width)
- x1 = cfg->width;
- if (y1 > cfg->height)
- y1 = cfg->height;
- for (y = y0; y < y1; y++) {
- const uint32_t *line = (const uint32_t *)
- ((const uint8_t *)src + (size_t)y * pitch);
- uint8_t *out = dst + y * stride + x0 / ppb;
- for (x = x0; x < x1; x += ppb) {
- uint8_t byte = 0;
- unsigned int i;
- for (i = 0; i < ppb; i++) {
- uint32_t c = line[x + i];
- unsigned int luma = (((c >> 16) & 0xff) * 77 +
- ((c >> 8) & 0xff) * 150 +
- (c & 0xff) * 29) >> 8;
- byte = (uint8_t)(byte << bpp) |
- ((255 - luma) >> (8 - bpp));
- }
- *out++ = byte;
- }
- }
- }
- #ifndef __KERNEL__ /* the driver has no use for it, so no div_u64() needed */
- uint64_t s1d_stats_bus_us(const struct s1d_stats *st, unsigned int scl_hz)
- {
- /* 9 clocks per byte, plus address byte and START/STOP per message */
- uint64_t clocks = ((uint64_t)st->i2c_bytes + st->i2c_msgs) * 9 +
- (uint64_t)st->i2c_msgs * 2;
- return scl_hz ? clocks * 1000000u / scl_hz : 0;
- }
- #endif
|