Home | History | Annotate | Line # | Download | only in spi
ssdfb_spi.c revision 1.12
      1  1.12  thorpej /* $NetBSD: ssdfb_spi.c,v 1.12 2022/01/19 05:05:45 thorpej Exp $ */
      2   1.1      tnn 
      3   1.1      tnn /*
      4   1.1      tnn  * Copyright (c) 2019 The NetBSD Foundation, Inc.
      5   1.1      tnn  * All rights reserved.
      6   1.1      tnn  *
      7   1.1      tnn  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1      tnn  * by Tobias Nygren.
      9   1.1      tnn  *
     10   1.1      tnn  * Redistribution and use in source and binary forms, with or without
     11   1.1      tnn  * modification, are permitted provided that the following conditions
     12   1.1      tnn  * are met:
     13   1.1      tnn  * 1. Redistributions of source code must retain the above copyright
     14   1.1      tnn  *    notice, this list of conditions and the following disclaimer.
     15   1.1      tnn  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1      tnn  *    notice, this list of conditions and the following disclaimer in the
     17   1.1      tnn  *    documentation and/or other materials provided with the distribution.
     18   1.1      tnn  *
     19   1.1      tnn  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1      tnn  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1      tnn  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1      tnn  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1      tnn  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1      tnn  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1      tnn  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1      tnn  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1      tnn  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1      tnn  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1      tnn  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1      tnn  */
     31   1.1      tnn 
     32   1.1      tnn #include <sys/cdefs.h>
     33  1.12  thorpej __KERNEL_RCSID(0, "$NetBSD: ssdfb_spi.c,v 1.12 2022/01/19 05:05:45 thorpej Exp $");
     34   1.1      tnn 
     35   1.1      tnn #include <sys/param.h>
     36   1.1      tnn #include <sys/device.h>
     37   1.1      tnn #include <sys/kernel.h>
     38   1.1      tnn #include <dev/wscons/wsdisplayvar.h>
     39   1.1      tnn #include <dev/rasops/rasops.h>
     40   1.1      tnn #include <dev/spi/spivar.h>
     41   1.1      tnn #include <dev/ic/ssdfbvar.h>
     42   1.6      tnn #include "opt_fdt.h"
     43   1.6      tnn #ifdef FDT
     44   1.6      tnn #include <dev/fdt/fdtvar.h>
     45   1.6      tnn #endif
     46   1.1      tnn 
     47   1.1      tnn struct bs_state {
     48   1.1      tnn 	uint8_t	*base;
     49   1.1      tnn 	uint8_t	*cur;
     50   1.1      tnn 	uint8_t	mask;
     51   1.1      tnn };
     52   1.1      tnn 
     53   1.1      tnn struct ssdfb_spi_softc {
     54   1.1      tnn 	struct ssdfb_softc	sc;
     55   1.1      tnn 	struct spi_handle	*sc_sh;
     56   1.6      tnn #ifdef FDT
     57   1.6      tnn 	struct fdtbus_gpio_pin	*sc_gpio_dc;
     58   1.7      tnn 	struct fdtbus_gpio_pin	*sc_gpio_res;
     59   1.6      tnn #endif
     60   1.1      tnn 	bool			sc_3wiremode;
     61  1.10      tnn 	bool			sc_late_dc_deassert;
     62  1.10      tnn 	uint8_t			sc_padding_cmd;
     63   1.1      tnn };
     64   1.1      tnn 
     65   1.1      tnn static int	ssdfb_spi_match(device_t, cfdata_t, void *);
     66   1.1      tnn static void	ssdfb_spi_attach(device_t, device_t, void *);
     67   1.1      tnn 
     68   1.1      tnn static int	ssdfb_spi_cmd_3wire(void *, uint8_t *, size_t, bool);
     69   1.1      tnn static int	ssdfb_spi_xfer_rect_3wire_ssd1322(void *, uint8_t, uint8_t,
     70   1.1      tnn 		    uint8_t, uint8_t, uint8_t *, size_t, bool);
     71   1.1      tnn 
     72   1.1      tnn static int	ssdfb_spi_cmd_4wire(void *, uint8_t *, size_t, bool);
     73  1.11      tnn static int	ssdfb_spi_xfer_rect_4wire_sh1106(void *, uint8_t, uint8_t,
     74  1.11      tnn 		    uint8_t, uint8_t, uint8_t *, size_t, bool);
     75  1.11      tnn static int	ssdfb_spi_xfer_rect_4wire_ssd1306(void *, uint8_t, uint8_t,
     76  1.11      tnn 		    uint8_t, uint8_t, uint8_t *, size_t, bool);
     77   1.1      tnn static int	ssdfb_spi_xfer_rect_4wire_ssd1322(void *, uint8_t, uint8_t,
     78   1.1      tnn 		    uint8_t, uint8_t, uint8_t *, size_t, bool);
     79   1.8      tnn static int	ssdfb_spi_xfer_rect_4wire_ssd1353(void *, uint8_t, uint8_t,
     80   1.8      tnn 		    uint8_t, uint8_t, uint8_t *, size_t, bool);
     81   1.1      tnn 
     82   1.1      tnn static void	ssdfb_bitstream_init(struct bs_state *, uint8_t *);
     83   1.1      tnn static void	ssdfb_bitstream_append(struct bs_state *, uint8_t, uint8_t);
     84   1.1      tnn static void	ssdfb_bitstream_append_cmd(struct bs_state *, uint8_t);
     85   1.1      tnn static void	ssdfb_bitstream_append_data(struct bs_state *, uint8_t *,
     86   1.1      tnn 		    size_t);
     87  1.10      tnn static void	ssdfb_bitstream_final(struct bs_state *, uint8_t);
     88   1.1      tnn 
     89   1.1      tnn CFATTACH_DECL_NEW(ssdfb_spi, sizeof(struct ssdfb_spi_softc),
     90   1.1      tnn     ssdfb_spi_match, ssdfb_spi_attach, NULL, NULL);
     91   1.1      tnn 
     92   1.3      tnn static const struct device_compatible_entry compat_data[] = {
     93   1.6      tnn 	{ .compat = "solomon,ssd1306",	.value = SSDFB_PRODUCT_SSD1306_GENERIC },
     94  1.11      tnn 	{ .compat = "sino,sh1106",	.value = SSDFB_PRODUCT_SH1106_GENERIC },
     95   1.6      tnn 	{ .compat = "solomon,ssd1322",	.value = SSDFB_PRODUCT_SSD1322_GENERIC },
     96   1.8      tnn 	{ .compat = "solomon,ssd1353",	.value = SSDFB_PRODUCT_SSD1353_GENERIC },
     97   1.8      tnn 	{ .compat = "dep160128a",	.value = SSDFB_PRODUCT_DEP_160128A_RGB },
     98   1.5  thorpej 	DEVICE_COMPAT_EOL
     99   1.3      tnn };
    100   1.3      tnn 
    101   1.1      tnn static int
    102   1.1      tnn ssdfb_spi_match(device_t parent, cfdata_t match, void *aux)
    103   1.1      tnn {
    104   1.1      tnn 	struct spi_attach_args *sa = aux;
    105   1.3      tnn 
    106  1.12  thorpej 	return spi_compatible_match(sa, match, compat_data);
    107   1.1      tnn }
    108   1.1      tnn 
    109   1.1      tnn static void
    110   1.1      tnn ssdfb_spi_attach(device_t parent, device_t self, void *aux)
    111   1.1      tnn {
    112   1.1      tnn 	struct ssdfb_spi_softc *sc = device_private(self);
    113   1.1      tnn 	struct cfdata *cf = device_cfdata(self);
    114   1.1      tnn 	struct spi_attach_args *sa = aux;
    115   1.1      tnn 	int flags = cf->cf_flags;
    116  1.12  thorpej 	int error;
    117   1.1      tnn 
    118   1.1      tnn 	sc->sc.sc_dev = self;
    119   1.1      tnn 	sc->sc_sh = sa->sa_handle;
    120   1.1      tnn 	sc->sc.sc_cookie = (void *)sc;
    121   1.6      tnn 	if ((flags & SSDFB_ATTACH_FLAG_PRODUCT_MASK) == SSDFB_PRODUCT_UNKNOWN) {
    122   1.6      tnn 		const struct device_compatible_entry *dce =
    123   1.6      tnn 			device_compatible_lookup(sa->sa_compat, sa->sa_ncompat, compat_data);
    124   1.6      tnn 		if (dce)
    125   1.6      tnn 			flags |= (int)dce->value;
    126   1.6      tnn 		else
    127   1.6      tnn 			flags |= SSDFB_PRODUCT_SSD1322_GENERIC;
    128   1.6      tnn 	}
    129  1.12  thorpej 
    130  1.12  thorpej 	/*
    131  1.12  thorpej 	 * SSD1306 and SSD1322 data sheets specify 100ns cycle time.
    132  1.12  thorpej 	 */
    133  1.12  thorpej 	error = spi_configure(sa->sa_handle, SPI_MODE_0, 10000000);
    134  1.12  thorpej 	if (error) {
    135  1.12  thorpej 		aprint_error(": failed to set Mode 0 @ 10MHz, error=%d\n",
    136  1.12  thorpej 		    error);
    137  1.12  thorpej 		return;
    138  1.12  thorpej 	}
    139  1.12  thorpej 
    140   1.1      tnn 	/*
    141   1.1      tnn 	 * Note on interface modes.
    142   1.1      tnn 	 *
    143   1.1      tnn 	 * 3 wire mode sends 9 bit sequences over the MOSI, MSB contains
    144   1.1      tnn 	 * the bit that determines if the lower 8 bits are command or data.
    145   1.1      tnn 	 *
    146   1.1      tnn 	 * 4 wire mode sends 8 bit sequences and requires an auxiliary GPIO
    147   1.6      tnn 	 * pin for the command/data bit.
    148   1.1      tnn 	 */
    149   1.6      tnn #ifdef FDT
    150   1.6      tnn 	const int phandle = sa->sa_cookie;
    151   1.7      tnn 	sc->sc_gpio_dc =
    152   1.7      tnn 	    fdtbus_gpio_acquire(phandle, "dc-gpio", GPIO_PIN_OUTPUT);
    153   1.6      tnn 	if (!sc->sc_gpio_dc)
    154   1.7      tnn 		sc->sc_gpio_dc =
    155   1.7      tnn 		    fdtbus_gpio_acquire(phandle, "cd-gpio", GPIO_PIN_OUTPUT);
    156   1.6      tnn 	sc->sc_3wiremode = (sc->sc_gpio_dc == NULL);
    157   1.7      tnn 	sc->sc_gpio_res =
    158   1.7      tnn 	    fdtbus_gpio_acquire(phandle, "res-gpio", GPIO_PIN_OUTPUT);
    159   1.7      tnn 	if (sc->sc_gpio_res) {
    160   1.7      tnn 		fdtbus_gpio_write_raw(sc->sc_gpio_res, 0);
    161   1.7      tnn 		DELAY(100);
    162   1.7      tnn 		fdtbus_gpio_write_raw(sc->sc_gpio_res, 1);
    163   1.7      tnn 		DELAY(100);
    164   1.7      tnn 	}
    165   1.6      tnn #else
    166   1.6      tnn 	sc->sc_3wiremode = true;
    167   1.6      tnn #endif
    168   1.1      tnn 
    169   1.8      tnn 	sc->sc.sc_cmd = sc->sc_3wiremode
    170   1.8      tnn 	    ? ssdfb_spi_cmd_3wire
    171   1.8      tnn 	    : ssdfb_spi_cmd_4wire;
    172   1.8      tnn 
    173   1.1      tnn 	switch (flags & SSDFB_ATTACH_FLAG_PRODUCT_MASK) {
    174  1.11      tnn 	case SSDFB_PRODUCT_SH1106_GENERIC:
    175  1.11      tnn 		sc->sc.sc_transfer_rect = sc->sc_3wiremode
    176  1.11      tnn 		    ? NULL
    177  1.11      tnn 		    : ssdfb_spi_xfer_rect_4wire_sh1106;
    178  1.11      tnn 		sc->sc_padding_cmd = SSDFB_CMD_NOP;
    179  1.11      tnn 		sc->sc_late_dc_deassert = true;
    180  1.11      tnn 		break;
    181  1.11      tnn 	case SSDFB_PRODUCT_SSD1306_GENERIC:
    182  1.11      tnn 		sc->sc.sc_transfer_rect = sc->sc_3wiremode
    183  1.11      tnn 		    ? NULL
    184  1.11      tnn 		    : ssdfb_spi_xfer_rect_4wire_ssd1306;
    185  1.11      tnn 		sc->sc_padding_cmd = SSDFB_CMD_NOP;
    186  1.11      tnn 		sc->sc_late_dc_deassert = true;
    187  1.11      tnn 		break;
    188   1.1      tnn 	case SSDFB_PRODUCT_SSD1322_GENERIC:
    189   1.8      tnn 		sc->sc.sc_transfer_rect = sc->sc_3wiremode
    190   1.8      tnn 		    ? ssdfb_spi_xfer_rect_3wire_ssd1322
    191   1.8      tnn 		    : ssdfb_spi_xfer_rect_4wire_ssd1322;
    192  1.10      tnn 		sc->sc_padding_cmd = SSD1322_CMD_WRITE_RAM;
    193   1.8      tnn 		break;
    194   1.8      tnn 	case SSDFB_PRODUCT_SSD1353_GENERIC:
    195   1.8      tnn 	case SSDFB_PRODUCT_DEP_160128A_RGB:
    196   1.8      tnn 		sc->sc.sc_transfer_rect = sc->sc_3wiremode
    197   1.8      tnn 		    ? NULL /* not supported here */
    198   1.8      tnn 		    : ssdfb_spi_xfer_rect_4wire_ssd1353;
    199   1.1      tnn 		break;
    200   1.1      tnn 	}
    201   1.8      tnn 
    202   1.8      tnn 	if (!sc->sc.sc_transfer_rect) {
    203   1.8      tnn 		aprint_error(": sc_transfer_rect not implemented\n");
    204   1.8      tnn 		return;
    205   1.1      tnn 	}
    206   1.6      tnn 
    207   1.1      tnn 	ssdfb_attach(&sc->sc, flags);
    208   1.1      tnn 
    209   1.8      tnn 	aprint_normal_dev(self, "%d-wire SPI interface\n",
    210   1.1      tnn 	    sc->sc_3wiremode == true ? 3 : 4);
    211   1.1      tnn }
    212   1.1      tnn 
    213   1.1      tnn static int
    214   1.1      tnn ssdfb_spi_cmd_3wire(void *cookie, uint8_t *cmd, size_t len, bool usepoll)
    215   1.1      tnn {
    216   1.1      tnn 	struct ssdfb_spi_softc *sc = (struct ssdfb_spi_softc *)cookie;
    217   1.1      tnn 	uint8_t bitstream[16 * 9 / 8];
    218   1.1      tnn 	struct bs_state s;
    219   1.1      tnn 
    220   1.1      tnn 	KASSERT(len > 0 && len <= 16);
    221   1.1      tnn 	ssdfb_bitstream_init(&s, bitstream);
    222   1.1      tnn 	ssdfb_bitstream_append_cmd(&s, *cmd);
    223   1.1      tnn 	cmd++;
    224   1.1      tnn 	len--;
    225   1.1      tnn 	ssdfb_bitstream_append_data(&s, cmd, len);
    226  1.10      tnn 	ssdfb_bitstream_final(&s, sc->sc_padding_cmd);
    227   1.1      tnn 
    228   1.1      tnn 	return spi_send(sc->sc_sh, s.cur - s.base, bitstream);
    229   1.1      tnn }
    230   1.1      tnn 
    231   1.1      tnn static int
    232   1.1      tnn ssdfb_spi_xfer_rect_3wire_ssd1322(void *cookie, uint8_t fromcol, uint8_t tocol,
    233   1.1      tnn     uint8_t fromrow, uint8_t torow, uint8_t *p, size_t stride, bool usepoll)
    234   1.1      tnn {
    235   1.1      tnn 	struct ssdfb_spi_softc *sc = (struct ssdfb_spi_softc *)cookie;
    236   1.1      tnn 	uint8_t bitstream[128 * 9 / 8];
    237   1.1      tnn 	struct bs_state s;
    238   1.1      tnn 	size_t rlen = (tocol + 1 - fromcol) * 2;
    239   1.1      tnn 	int error;
    240   1.1      tnn 
    241   1.1      tnn 	/*
    242   1.1      tnn 	 * Unlike iic(4), there is no way to force spi(4) to use polling.
    243   1.1      tnn 	 */
    244   1.2      tnn 	if (usepoll && !cold)
    245   1.1      tnn 		return 0;
    246   1.1      tnn 
    247   1.1      tnn 	ssdfb_bitstream_init(&s, bitstream);
    248   1.1      tnn 	ssdfb_bitstream_append_cmd(&s, SSD1322_CMD_SET_ROW_ADDRESS);
    249   1.1      tnn 	ssdfb_bitstream_append_data(&s, &fromrow, 1);
    250   1.1      tnn 	ssdfb_bitstream_append_data(&s, &torow, 1);
    251   1.1      tnn 	ssdfb_bitstream_append_cmd(&s, SSD1322_CMD_SET_COLUMN_ADDRESS);
    252   1.1      tnn 	ssdfb_bitstream_append_data(&s, &fromcol, 1);
    253   1.1      tnn 	ssdfb_bitstream_append_data(&s, &tocol, 1);
    254   1.1      tnn 	ssdfb_bitstream_append_cmd(&s, SSD1322_CMD_WRITE_RAM);
    255  1.10      tnn 	ssdfb_bitstream_final(&s, sc->sc_padding_cmd);
    256   1.1      tnn 	error = spi_send(sc->sc_sh, s.cur - s.base, bitstream);
    257   1.1      tnn 	if (error)
    258   1.1      tnn 		return error;
    259   1.1      tnn 
    260   1.1      tnn 	KASSERT(rlen <= 128);
    261  1.11      tnn 	while (fromrow <= torow) {
    262   1.1      tnn 		ssdfb_bitstream_init(&s, bitstream);
    263   1.1      tnn 		ssdfb_bitstream_append_data(&s, p, rlen);
    264  1.10      tnn 		ssdfb_bitstream_final(&s, sc->sc_padding_cmd);
    265   1.1      tnn 		error = spi_send(sc->sc_sh, s.cur - s.base, bitstream);
    266   1.1      tnn 		if (error)
    267   1.1      tnn 			return error;
    268  1.11      tnn 		fromrow++;
    269   1.1      tnn 		p += stride;
    270   1.1      tnn 	}
    271   1.1      tnn 
    272   1.1      tnn 	return 0;
    273   1.1      tnn }
    274   1.1      tnn 
    275   1.1      tnn static void
    276   1.1      tnn ssdfb_bitstream_init(struct bs_state *s, uint8_t *dst)
    277   1.1      tnn {
    278   1.1      tnn 	s->base = s->cur = dst;
    279   1.1      tnn 	s->mask = 0x80;
    280   1.1      tnn }
    281   1.1      tnn 
    282   1.1      tnn static void
    283   1.1      tnn ssdfb_bitstream_append(struct bs_state *s, uint8_t b, uint8_t srcmask)
    284   1.1      tnn {
    285   1.1      tnn 	while(srcmask) {
    286   1.1      tnn 		if (b & srcmask)
    287   1.1      tnn 			*s->cur |= s->mask;
    288   1.1      tnn 		else
    289   1.1      tnn 			*s->cur &= ~s->mask;
    290   1.1      tnn 		srcmask >>= 1;
    291   1.1      tnn 		s->mask >>= 1;
    292   1.1      tnn 		if (!s->mask) {
    293   1.1      tnn 			s->mask = 0x80;
    294   1.1      tnn 			s->cur++;
    295   1.1      tnn 		}
    296   1.1      tnn 	}
    297   1.1      tnn }
    298   1.1      tnn 
    299   1.1      tnn static void
    300   1.1      tnn ssdfb_bitstream_append_cmd(struct bs_state *s, uint8_t cmd)
    301   1.1      tnn {
    302   1.1      tnn 	ssdfb_bitstream_append(s, 0, 1);
    303   1.1      tnn 	ssdfb_bitstream_append(s, cmd, 0x80);
    304   1.1      tnn }
    305   1.1      tnn 
    306   1.1      tnn static void
    307   1.1      tnn ssdfb_bitstream_append_data(struct bs_state *s, uint8_t *data, size_t len)
    308   1.1      tnn {
    309   1.1      tnn 	while(len--) {
    310   1.1      tnn 		ssdfb_bitstream_append(s, 1, 1);
    311   1.1      tnn 		ssdfb_bitstream_append(s, *data++, 0x80);
    312   1.1      tnn 	}
    313   1.1      tnn }
    314   1.1      tnn 
    315   1.1      tnn static void
    316  1.10      tnn ssdfb_bitstream_final(struct bs_state *s, uint8_t padding_cmd)
    317   1.1      tnn {
    318   1.1      tnn 	while (s->mask != 0x80) {
    319   1.1      tnn 		ssdfb_bitstream_append_cmd(s, padding_cmd);
    320   1.1      tnn 	}
    321   1.1      tnn }
    322   1.1      tnn 
    323   1.1      tnn static void
    324   1.1      tnn ssdfb_spi_4wire_set_dc(struct ssdfb_spi_softc *sc, int value)
    325   1.1      tnn {
    326   1.6      tnn #ifdef FDT
    327   1.6      tnn 	fdtbus_gpio_write_raw(sc->sc_gpio_dc, value);
    328   1.6      tnn #else
    329   1.1      tnn 	panic("ssdfb_spi_4wire_set_dc");
    330   1.6      tnn #endif
    331   1.1      tnn }
    332   1.1      tnn 
    333   1.1      tnn static int
    334   1.1      tnn ssdfb_spi_cmd_4wire(void *cookie, uint8_t *cmd, size_t len, bool usepoll)
    335   1.1      tnn {
    336   1.1      tnn 	struct ssdfb_spi_softc *sc = (struct ssdfb_spi_softc *)cookie;
    337   1.1      tnn 	int error;
    338   1.1      tnn 
    339   1.1      tnn 	ssdfb_spi_4wire_set_dc(sc, 0);
    340   1.1      tnn 	error = spi_send(sc->sc_sh, 1, cmd);
    341   1.1      tnn 	if (error)
    342   1.1      tnn 		return error;
    343   1.1      tnn 	if (len > 1) {
    344  1.10      tnn 		if (!sc->sc_late_dc_deassert)
    345  1.10      tnn 			ssdfb_spi_4wire_set_dc(sc, 1);
    346   1.1      tnn 		len--;
    347   1.1      tnn 		cmd++;
    348   1.1      tnn 		error = spi_send(sc->sc_sh, len, cmd);
    349   1.1      tnn 		if (error)
    350   1.1      tnn 			return error;
    351   1.1      tnn 	}
    352   1.1      tnn 
    353   1.1      tnn 	return 0;
    354   1.1      tnn }
    355   1.1      tnn 
    356   1.1      tnn static int
    357  1.11      tnn ssdfb_spi_xfer_rect_4wire_sh1106(void *cookie, uint8_t fromcol, uint8_t tocol,
    358  1.11      tnn     uint8_t frompage, uint8_t topage, uint8_t *p, size_t stride, bool usepoll)
    359  1.11      tnn {
    360  1.11      tnn 	struct ssdfb_spi_softc *sc = (struct ssdfb_spi_softc *)cookie;
    361  1.11      tnn 	size_t rlen = tocol + 1 - fromcol;
    362  1.11      tnn 	int error;
    363  1.11      tnn 	uint8_t cmd[] = {
    364  1.11      tnn 		SSDFB_CMD_SET_PAGE_START_ADDRESS_BASE + frompage,
    365  1.11      tnn 		SSDFB_CMD_SET_HIGHER_COLUMN_START_ADDRESS_BASE + (fromcol >> 4),
    366  1.11      tnn 		SSDFB_CMD_SET_LOWER_COLUMN_START_ADDRESS_BASE + (fromcol & 0xf)
    367  1.11      tnn 	};
    368  1.11      tnn 
    369  1.11      tnn 	if (usepoll && !cold)
    370  1.11      tnn 		return 0;
    371  1.11      tnn 
    372  1.11      tnn 	while (frompage <= topage) {
    373  1.11      tnn 		cmd[0] = SSDFB_CMD_SET_PAGE_START_ADDRESS_BASE + frompage;
    374  1.11      tnn 		ssdfb_spi_4wire_set_dc(sc, 0);
    375  1.11      tnn 		error = spi_send(sc->sc_sh, sizeof(cmd), cmd);
    376  1.11      tnn 		if (error)
    377  1.11      tnn 			return error;
    378  1.11      tnn 		ssdfb_spi_4wire_set_dc(sc, 1);
    379  1.11      tnn 		error = spi_send(sc->sc_sh, rlen, p);
    380  1.11      tnn 		if (error)
    381  1.11      tnn 			return error;
    382  1.11      tnn 		frompage++;
    383  1.11      tnn 		p += stride;
    384  1.11      tnn 	}
    385  1.11      tnn 
    386  1.11      tnn 	return 0;
    387  1.11      tnn }
    388  1.11      tnn 
    389  1.11      tnn static int
    390  1.11      tnn ssdfb_spi_xfer_rect_4wire_ssd1306(void *cookie, uint8_t fromcol, uint8_t tocol,
    391  1.11      tnn     uint8_t frompage, uint8_t topage, uint8_t *p, size_t stride, bool usepoll)
    392  1.11      tnn {
    393  1.11      tnn 	struct ssdfb_spi_softc *sc = (struct ssdfb_spi_softc *)cookie;
    394  1.11      tnn 	size_t rlen = tocol + 1 - fromcol;
    395  1.11      tnn 	int error;
    396  1.11      tnn 	uint8_t cmd[] = {
    397  1.11      tnn 		SSD1306_CMD_SET_MEMORY_ADDRESSING_MODE,
    398  1.11      tnn 		SSD1306_MEMORY_ADDRESSING_MODE_HORIZONTAL,
    399  1.11      tnn 		SSD1306_CMD_SET_COLUMN_ADDRESS,
    400  1.11      tnn 		fromcol,
    401  1.11      tnn 		tocol,
    402  1.11      tnn 		SSD1306_CMD_SET_PAGE_ADDRESS,
    403  1.11      tnn 		frompage,
    404  1.11      tnn 		topage
    405  1.11      tnn 	};
    406  1.11      tnn 
    407  1.11      tnn 	if (usepoll && !cold)
    408  1.11      tnn 		return 0;
    409  1.11      tnn 
    410  1.11      tnn 	ssdfb_spi_4wire_set_dc(sc, 0);
    411  1.11      tnn 	error = spi_send(sc->sc_sh, sizeof(cmd), cmd);
    412  1.11      tnn 	if (error)
    413  1.11      tnn 		return error;
    414  1.11      tnn 	ssdfb_spi_4wire_set_dc(sc, 1);
    415  1.11      tnn 
    416  1.11      tnn 	while (frompage <= topage) {
    417  1.11      tnn 		error = spi_send(sc->sc_sh, rlen, p);
    418  1.11      tnn 		if (error)
    419  1.11      tnn 			return error;
    420  1.11      tnn 		frompage++;
    421  1.11      tnn 		p += stride;
    422  1.11      tnn 	}
    423  1.11      tnn 
    424  1.11      tnn 	return 0;
    425  1.11      tnn }
    426  1.11      tnn 
    427  1.11      tnn static int
    428   1.1      tnn ssdfb_spi_xfer_rect_4wire_ssd1322(void *cookie, uint8_t fromcol, uint8_t tocol,
    429   1.1      tnn     uint8_t fromrow, uint8_t torow, uint8_t *p, size_t stride, bool usepoll)
    430   1.1      tnn {
    431   1.1      tnn 	struct ssdfb_spi_softc *sc = (struct ssdfb_spi_softc *)cookie;
    432   1.1      tnn 	size_t rlen = (tocol + 1 - fromcol) * 2;
    433   1.1      tnn 	int error;
    434   1.1      tnn 	uint8_t cmd;
    435   1.1      tnn 	uint8_t data[2];
    436   1.1      tnn 
    437   1.2      tnn 	if (usepoll && !cold)
    438   1.1      tnn 		return 0;
    439   1.1      tnn 
    440   1.1      tnn 	ssdfb_spi_4wire_set_dc(sc, 0);
    441   1.1      tnn 	cmd = SSD1322_CMD_SET_ROW_ADDRESS;
    442   1.1      tnn 	error = spi_send(sc->sc_sh, sizeof(cmd), &cmd);
    443   1.1      tnn 	if (error)
    444   1.1      tnn 		return error;
    445   1.1      tnn 	ssdfb_spi_4wire_set_dc(sc, 1);
    446   1.1      tnn 	data[0] = fromrow;
    447   1.1      tnn 	data[1] = torow;
    448   1.1      tnn 	error = spi_send(sc->sc_sh, sizeof(data), data);
    449   1.1      tnn 	if (error)
    450   1.1      tnn 		return error;
    451   1.1      tnn 
    452   1.1      tnn 	ssdfb_spi_4wire_set_dc(sc, 0);
    453   1.1      tnn 	cmd = SSD1322_CMD_SET_COLUMN_ADDRESS;
    454   1.1      tnn 	error = spi_send(sc->sc_sh, sizeof(cmd), &cmd);
    455   1.1      tnn 	if (error)
    456   1.1      tnn 		return error;
    457   1.1      tnn 	ssdfb_spi_4wire_set_dc(sc, 1);
    458   1.1      tnn 	data[0] = fromcol;
    459   1.1      tnn 	data[1] = tocol;
    460   1.1      tnn 	error = spi_send(sc->sc_sh, sizeof(data), data);
    461   1.1      tnn 	if (error)
    462   1.1      tnn 		return error;
    463   1.1      tnn 
    464   1.1      tnn 	ssdfb_spi_4wire_set_dc(sc, 0);
    465   1.1      tnn 	cmd = SSD1322_CMD_WRITE_RAM;
    466   1.1      tnn 	error = spi_send(sc->sc_sh, sizeof(cmd), &cmd);
    467   1.1      tnn 	if (error)
    468   1.1      tnn 		return error;
    469   1.1      tnn 
    470   1.1      tnn 	ssdfb_spi_4wire_set_dc(sc, 1);
    471  1.11      tnn 	while (fromrow <= torow) {
    472   1.1      tnn 		error = spi_send(sc->sc_sh, rlen, p);
    473   1.1      tnn 		if (error)
    474   1.1      tnn 			return error;
    475  1.11      tnn 		fromrow++;
    476   1.1      tnn 		p += stride;
    477   1.1      tnn 	}
    478   1.1      tnn 
    479   1.1      tnn 	return 0;
    480   1.1      tnn }
    481   1.8      tnn 
    482   1.8      tnn static int
    483   1.8      tnn ssdfb_spi_xfer_rect_4wire_ssd1353(void *cookie, uint8_t fromcol, uint8_t tocol,
    484   1.8      tnn     uint8_t fromrow, uint8_t torow, uint8_t *p, size_t stride, bool usepoll)
    485   1.8      tnn {
    486   1.8      tnn 	struct ssdfb_spi_softc *sc = (struct ssdfb_spi_softc *)cookie;
    487   1.8      tnn 	size_t rlen = (tocol + 1 - fromcol) * 3;
    488   1.8      tnn 	uint8_t bitstream[160 * 3];
    489   1.8      tnn 	uint8_t *dstp, *srcp, *endp;
    490   1.8      tnn 	int error;
    491   1.8      tnn 	uint8_t cmd;
    492   1.8      tnn 	uint8_t data[2];
    493   1.8      tnn 
    494   1.8      tnn 	if (usepoll && !cold)
    495   1.8      tnn 		return 0;
    496   1.8      tnn 
    497   1.8      tnn 	ssdfb_spi_4wire_set_dc(sc, 0);
    498   1.9      tnn 	cmd = SSD1353_CMD_SET_ROW_ADDRESS;
    499   1.8      tnn 	error = spi_send(sc->sc_sh, sizeof(cmd), &cmd);
    500   1.8      tnn 	if (error)
    501   1.8      tnn 		return error;
    502   1.8      tnn 	ssdfb_spi_4wire_set_dc(sc, 1);
    503   1.8      tnn 	data[0] = fromrow;
    504   1.8      tnn 	data[1] = torow;
    505   1.8      tnn 	if (sc->sc.sc_upsidedown) {
    506   1.8      tnn 		/* fix picture outside frame on 160x128 panel */
    507   1.8      tnn 		data[0] += 132 - sc->sc.sc_p->p_height;
    508   1.8      tnn 		data[1] += 132 - sc->sc.sc_p->p_height;
    509   1.8      tnn 	}
    510   1.8      tnn 	error = spi_send(sc->sc_sh, sizeof(data), data);
    511   1.8      tnn 	if (error)
    512   1.8      tnn 		return error;
    513   1.8      tnn 
    514   1.8      tnn 	ssdfb_spi_4wire_set_dc(sc, 0);
    515   1.9      tnn 	cmd = SSD1353_CMD_SET_COLUMN_ADDRESS;
    516   1.8      tnn 	error = spi_send(sc->sc_sh, sizeof(cmd), &cmd);
    517   1.8      tnn 	if (error)
    518   1.8      tnn 		return error;
    519   1.8      tnn 	ssdfb_spi_4wire_set_dc(sc, 1);
    520   1.8      tnn 	data[0] = fromcol;
    521   1.8      tnn 	data[1] = tocol;
    522   1.8      tnn 	error = spi_send(sc->sc_sh, sizeof(data), data);
    523   1.8      tnn 	if (error)
    524   1.8      tnn 		return error;
    525   1.8      tnn 
    526   1.8      tnn 	ssdfb_spi_4wire_set_dc(sc, 0);
    527   1.9      tnn 	cmd = SSD1353_CMD_WRITE_RAM;
    528   1.8      tnn 	error = spi_send(sc->sc_sh, sizeof(cmd), &cmd);
    529   1.8      tnn 	if (error)
    530   1.8      tnn 		return error;
    531   1.8      tnn 
    532   1.8      tnn 	ssdfb_spi_4wire_set_dc(sc, 1);
    533   1.8      tnn 	KASSERT(rlen <= sizeof(bitstream));
    534  1.11      tnn 	while (fromrow <= torow) {
    535   1.8      tnn 		/* downconvert each row from 32bpp rgba to 18bpp panel format */
    536   1.8      tnn 		dstp = bitstream;
    537   1.8      tnn 		endp = dstp + rlen;
    538   1.8      tnn 		srcp = p;
    539   1.8      tnn 		while (dstp < endp) {
    540   1.8      tnn 			*dstp++ = (*srcp++) >> 2;
    541   1.8      tnn 			*dstp++ = (*srcp++) >> 2;
    542   1.8      tnn 			*dstp++ = (*srcp++) >> 2;
    543   1.8      tnn 			srcp++;
    544   1.8      tnn 		}
    545   1.8      tnn 		error = spi_send(sc->sc_sh, rlen, bitstream);
    546   1.8      tnn 		if (error)
    547   1.8      tnn 			return error;
    548  1.11      tnn 		fromrow++;
    549   1.8      tnn 		p += stride;
    550   1.8      tnn 	}
    551   1.8      tnn 
    552   1.8      tnn 	return 0;
    553   1.8      tnn }
    554