Home | History | Annotate | Line # | Download | only in spi
ssdfb_spi.c revision 1.5.4.3
      1  1.5.4.3  thorpej /* $NetBSD: ssdfb_spi.c,v 1.5.4.3 2021/08/01 22:42:31 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.5.4.3  thorpej __KERNEL_RCSID(0, "$NetBSD: ssdfb_spi.c,v 1.5.4.3 2021/08/01 22:42:31 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.5.4.2  thorpej 
     41  1.5.4.3  thorpej #include <dev/spi/spivar.h>
     42  1.5.4.3  thorpej #include <dev/ic/ssdfbvar.h>
     43  1.5.4.3  thorpej #include "opt_fdt.h"
     44  1.5.4.2  thorpej #ifdef FDT
     45  1.5.4.2  thorpej #include <dev/fdt/fdtvar.h>
     46  1.5.4.2  thorpej #endif /* FDT */
     47  1.5.4.2  thorpej 
     48      1.1      tnn struct bs_state {
     49      1.1      tnn 	uint8_t	*base;
     50      1.1      tnn 	uint8_t	*cur;
     51      1.1      tnn 	uint8_t	mask;
     52      1.1      tnn };
     53      1.1      tnn 
     54      1.1      tnn struct ssdfb_spi_softc {
     55      1.1      tnn 	struct ssdfb_softc	sc;
     56      1.1      tnn 	struct spi_handle	*sc_sh;
     57  1.5.4.2  thorpej #ifdef FDT
     58  1.5.4.3  thorpej 	struct fdtbus_gpio_pin	*sc_gpio_dc;
     59  1.5.4.2  thorpej #endif /* FDT */
     60      1.1      tnn 	bool			sc_3wiremode;
     61      1.1      tnn };
     62      1.1      tnn 
     63      1.1      tnn static int	ssdfb_spi_match(device_t, cfdata_t, void *);
     64      1.1      tnn static void	ssdfb_spi_attach(device_t, device_t, void *);
     65      1.1      tnn 
     66      1.1      tnn static int	ssdfb_spi_cmd_3wire(void *, uint8_t *, size_t, bool);
     67      1.1      tnn static int	ssdfb_spi_xfer_rect_3wire_ssd1322(void *, uint8_t, uint8_t,
     68      1.1      tnn 		    uint8_t, uint8_t, uint8_t *, size_t, bool);
     69      1.1      tnn 
     70      1.1      tnn static int	ssdfb_spi_cmd_4wire(void *, uint8_t *, size_t, bool);
     71      1.1      tnn static int	ssdfb_spi_xfer_rect_4wire_ssd1322(void *, uint8_t, uint8_t,
     72      1.1      tnn 		    uint8_t, uint8_t, uint8_t *, size_t, bool);
     73      1.1      tnn 
     74      1.1      tnn static void	ssdfb_bitstream_init(struct bs_state *, uint8_t *);
     75      1.1      tnn static void	ssdfb_bitstream_append(struct bs_state *, uint8_t, uint8_t);
     76      1.1      tnn static void	ssdfb_bitstream_append_cmd(struct bs_state *, uint8_t);
     77      1.1      tnn static void	ssdfb_bitstream_append_data(struct bs_state *, uint8_t *,
     78      1.1      tnn 		    size_t);
     79      1.1      tnn static void	ssdfb_bitstream_final(struct bs_state *);
     80      1.1      tnn 
     81      1.1      tnn CFATTACH_DECL_NEW(ssdfb_spi, sizeof(struct ssdfb_spi_softc),
     82      1.1      tnn     ssdfb_spi_match, ssdfb_spi_attach, NULL, NULL);
     83      1.1      tnn 
     84      1.3      tnn static const struct device_compatible_entry compat_data[] = {
     85  1.5.4.3  thorpej 	{ .compat = "solomon,ssd1306",	.value = SSDFB_PRODUCT_SSD1306_GENERIC },
     86  1.5.4.3  thorpej 	{ .compat = "solomon,ssd1322",	.value = SSDFB_PRODUCT_SSD1322_GENERIC },
     87      1.5  thorpej 	DEVICE_COMPAT_EOL
     88      1.3      tnn };
     89      1.3      tnn 
     90      1.1      tnn static int
     91      1.1      tnn ssdfb_spi_match(device_t parent, cfdata_t match, void *aux)
     92      1.1      tnn {
     93      1.1      tnn 	struct spi_attach_args *sa = aux;
     94      1.3      tnn 
     95  1.5.4.1  thorpej 	return spi_compatible_match(sa, match, compat_data);
     96      1.1      tnn }
     97      1.1      tnn 
     98  1.5.4.2  thorpej #ifdef FDT
     99  1.5.4.2  thorpej static void
    100  1.5.4.2  thorpej ssdfb_spi_dc_gpio_fdt(struct ssdfb_spi_softc *sc)
    101  1.5.4.2  thorpej {
    102  1.5.4.2  thorpej 	devhandle_t devhandle = device_handle(sc->sc.sc_dev);
    103  1.5.4.2  thorpej 	int phandle = devhandle_to_of(devhandle);
    104  1.5.4.2  thorpej 
    105  1.5.4.3  thorpej 	sc->sc_gpio_dc = fdtbus_gpio_acquire(phandle, "dc-gpio",
    106  1.5.4.2  thorpej 	    GPIO_PIN_OUTPUT);
    107  1.5.4.3  thorpej 	if (sc->sc_dc_gpio == NULL) {
    108  1.5.4.3  thorpej 		sc->sc_gpio_dc = fdtbus_gpio_acquire(phandle, "cd-gpio",
    109  1.5.4.3  thorpej 		    GPIO_PIN_OUTPUT);
    110  1.5.4.3  thorpej 	}
    111  1.5.4.2  thorpej 	if (sc->sc_dc_gpio != NULL) {
    112  1.5.4.2  thorpej 		sc->sc_3wiremode = false;
    113  1.5.4.2  thorpej 	}
    114  1.5.4.2  thorpej }
    115  1.5.4.2  thorpej #endif /* FDT */
    116  1.5.4.2  thorpej 
    117      1.1      tnn static void
    118      1.1      tnn ssdfb_spi_attach(device_t parent, device_t self, void *aux)
    119      1.1      tnn {
    120      1.1      tnn 	struct ssdfb_spi_softc *sc = device_private(self);
    121  1.5.4.2  thorpej 	devhandle_t devhandle = device_handle(self);
    122      1.1      tnn 	struct cfdata *cf = device_cfdata(self);
    123      1.1      tnn 	struct spi_attach_args *sa = aux;
    124      1.1      tnn 	int flags = cf->cf_flags;
    125  1.5.4.1  thorpej 	int error;
    126      1.1      tnn 
    127      1.1      tnn 	sc->sc.sc_dev = self;
    128      1.1      tnn 	sc->sc_sh = sa->sa_handle;
    129      1.1      tnn 	sc->sc.sc_cookie = (void *)sc;
    130  1.5.4.2  thorpej 
    131  1.5.4.1  thorpej 	/*
    132  1.5.4.1  thorpej 	 * SSD1306 and SSD1322 data sheets specify 100ns cycle time.
    133  1.5.4.1  thorpej 	 */
    134  1.5.4.1  thorpej 	error = spi_configure(sa->sa_handle, SPI_MODE_0, 10000000);
    135  1.5.4.1  thorpej 	if (error) {
    136  1.5.4.1  thorpej 		aprint_error(": spi_configure failed (error = %d)\n",
    137  1.5.4.1  thorpej 		    error);
    138  1.5.4.1  thorpej 		return;
    139  1.5.4.1  thorpej 	}
    140  1.5.4.1  thorpej 
    141  1.5.4.3  thorpej 	if ((flags & SSDFB_ATTACH_FLAG_PRODUCT_MASK) == SSDFB_PRODUCT_UNKNOWN) {
    142  1.5.4.3  thorpej 		const struct device_compatible_entry *dce =
    143  1.5.4.3  thorpej 			device_compatible_lookup(sa->sa_compat, sa->sa_ncompat,
    144  1.5.4.3  thorpej 						 compat_data);
    145  1.5.4.3  thorpej 		if (dce)
    146  1.5.4.3  thorpej 			flags |= (int)dce->value;
    147  1.5.4.3  thorpej 		else
    148  1.5.4.3  thorpej 			flags |= SSDFB_PRODUCT_SSD1322_GENERIC;
    149  1.5.4.3  thorpej 	}
    150  1.5.4.3  thorpej 
    151      1.1      tnn 	/*
    152      1.1      tnn 	 * Note on interface modes.
    153      1.1      tnn 	 *
    154      1.1      tnn 	 * 3 wire mode sends 9 bit sequences over the MOSI, MSB contains
    155      1.1      tnn 	 * the bit that determines if the lower 8 bits are command or data.
    156      1.1      tnn 	 *
    157      1.1      tnn 	 * 4 wire mode sends 8 bit sequences and requires an auxiliary GPIO
    158  1.5.4.2  thorpej 	 * pin for the command/data bit.
    159  1.5.4.2  thorpej 	 *
    160  1.5.4.2  thorpej 	 * Default to 3 wire mode.  If the device tree specifies a
    161  1.5.4.2  thorpej 	 * D/C GPIO pin, then we will use 4 wire mode.
    162      1.1      tnn 	 */
    163      1.1      tnn 	sc->sc_3wiremode = true;
    164  1.5.4.2  thorpej 	switch (devhandle_type(devhandle)) {
    165  1.5.4.2  thorpej #ifdef FDT
    166  1.5.4.2  thorpej 	case DEVHANDLE_TYPE_OF:
    167  1.5.4.2  thorpej 		ssdfb_spi_dc_gpio_fdt(sc);
    168  1.5.4.2  thorpej 		break;
    169  1.5.4.2  thorpej #endif /* FDT */
    170  1.5.4.2  thorpej 	default:
    171  1.5.4.2  thorpej 		break;
    172  1.5.4.2  thorpej 	}
    173      1.1      tnn 
    174      1.1      tnn 	switch (flags & SSDFB_ATTACH_FLAG_PRODUCT_MASK) {
    175      1.1      tnn 	case SSDFB_PRODUCT_SSD1322_GENERIC:
    176      1.1      tnn 		if (sc->sc_3wiremode) {
    177      1.1      tnn 			sc->sc.sc_transfer_rect =
    178      1.1      tnn 			    ssdfb_spi_xfer_rect_3wire_ssd1322;
    179      1.1      tnn 		} else {
    180      1.1      tnn 			sc->sc.sc_transfer_rect =
    181      1.1      tnn 			    ssdfb_spi_xfer_rect_4wire_ssd1322;
    182      1.1      tnn 		}
    183      1.1      tnn 		break;
    184      1.1      tnn 	default:
    185      1.1      tnn 		panic("ssdfb_spi_attach: product not implemented");
    186      1.1      tnn 	}
    187      1.1      tnn 	if (sc->sc_3wiremode) {
    188      1.1      tnn 		sc->sc.sc_cmd = ssdfb_spi_cmd_3wire;
    189      1.1      tnn 	} else {
    190      1.1      tnn 		sc->sc.sc_cmd = ssdfb_spi_cmd_4wire;
    191      1.1      tnn 	}
    192  1.5.4.3  thorpej 
    193      1.1      tnn 	ssdfb_attach(&sc->sc, flags);
    194      1.1      tnn 
    195      1.1      tnn 	device_printf(sc->sc.sc_dev, "%d-wire SPI interface\n",
    196      1.1      tnn 	    sc->sc_3wiremode == true ? 3 : 4);
    197      1.1      tnn }
    198      1.1      tnn 
    199      1.1      tnn static int
    200      1.1      tnn ssdfb_spi_cmd_3wire(void *cookie, uint8_t *cmd, size_t len, bool usepoll)
    201      1.1      tnn {
    202      1.1      tnn 	struct ssdfb_spi_softc *sc = (struct ssdfb_spi_softc *)cookie;
    203      1.1      tnn 	uint8_t bitstream[16 * 9 / 8];
    204      1.1      tnn 	struct bs_state s;
    205      1.1      tnn 
    206      1.1      tnn 	KASSERT(len > 0 && len <= 16);
    207      1.1      tnn 	ssdfb_bitstream_init(&s, bitstream);
    208      1.1      tnn 	ssdfb_bitstream_append_cmd(&s, *cmd);
    209      1.1      tnn 	cmd++;
    210      1.1      tnn 	len--;
    211      1.1      tnn 	ssdfb_bitstream_append_data(&s, cmd, len);
    212      1.1      tnn 	ssdfb_bitstream_final(&s);
    213      1.1      tnn 
    214      1.1      tnn 	return spi_send(sc->sc_sh, s.cur - s.base, bitstream);
    215      1.1      tnn }
    216      1.1      tnn 
    217      1.1      tnn static int
    218      1.1      tnn ssdfb_spi_xfer_rect_3wire_ssd1322(void *cookie, uint8_t fromcol, uint8_t tocol,
    219      1.1      tnn     uint8_t fromrow, uint8_t torow, uint8_t *p, size_t stride, bool usepoll)
    220      1.1      tnn {
    221      1.1      tnn 	struct ssdfb_spi_softc *sc = (struct ssdfb_spi_softc *)cookie;
    222      1.1      tnn 	uint8_t bitstream[128 * 9 / 8];
    223      1.1      tnn 	struct bs_state s;
    224      1.1      tnn 	uint8_t row;
    225      1.1      tnn 	size_t rlen = (tocol + 1 - fromcol) * 2;
    226      1.1      tnn 	int error;
    227      1.1      tnn 
    228      1.1      tnn 	/*
    229      1.1      tnn 	 * Unlike iic(4), there is no way to force spi(4) to use polling.
    230      1.1      tnn 	 */
    231      1.2      tnn 	if (usepoll && !cold)
    232      1.1      tnn 		return 0;
    233      1.1      tnn 
    234      1.1      tnn 	ssdfb_bitstream_init(&s, bitstream);
    235      1.1      tnn 	ssdfb_bitstream_append_cmd(&s, SSD1322_CMD_SET_ROW_ADDRESS);
    236      1.1      tnn 	ssdfb_bitstream_append_data(&s, &fromrow, 1);
    237      1.1      tnn 	ssdfb_bitstream_append_data(&s, &torow, 1);
    238      1.1      tnn 	ssdfb_bitstream_append_cmd(&s, SSD1322_CMD_SET_COLUMN_ADDRESS);
    239      1.1      tnn 	ssdfb_bitstream_append_data(&s, &fromcol, 1);
    240      1.1      tnn 	ssdfb_bitstream_append_data(&s, &tocol, 1);
    241      1.1      tnn 	ssdfb_bitstream_append_cmd(&s, SSD1322_CMD_WRITE_RAM);
    242      1.1      tnn 	ssdfb_bitstream_final(&s);
    243      1.1      tnn 	error = spi_send(sc->sc_sh, s.cur - s.base, bitstream);
    244      1.1      tnn 	if (error)
    245      1.1      tnn 		return error;
    246      1.1      tnn 
    247      1.1      tnn 	KASSERT(rlen <= 128);
    248      1.1      tnn 	for (row = fromrow; row <= torow; row++) {
    249      1.1      tnn 		ssdfb_bitstream_init(&s, bitstream);
    250      1.1      tnn 		ssdfb_bitstream_append_data(&s, p, rlen);
    251      1.1      tnn 		ssdfb_bitstream_final(&s);
    252      1.1      tnn 		error = spi_send(sc->sc_sh, s.cur - s.base, bitstream);
    253      1.1      tnn 		if (error)
    254      1.1      tnn 			return error;
    255      1.1      tnn 		p += stride;
    256      1.1      tnn 	}
    257      1.1      tnn 
    258      1.1      tnn 	return 0;
    259      1.1      tnn }
    260      1.1      tnn 
    261      1.1      tnn static void
    262      1.1      tnn ssdfb_bitstream_init(struct bs_state *s, uint8_t *dst)
    263      1.1      tnn {
    264      1.1      tnn 	s->base = s->cur = dst;
    265      1.1      tnn 	s->mask = 0x80;
    266      1.1      tnn }
    267      1.1      tnn 
    268      1.1      tnn static void
    269      1.1      tnn ssdfb_bitstream_append(struct bs_state *s, uint8_t b, uint8_t srcmask)
    270      1.1      tnn {
    271      1.1      tnn 	while(srcmask) {
    272      1.1      tnn 		if (b & srcmask)
    273      1.1      tnn 			*s->cur |= s->mask;
    274      1.1      tnn 		else
    275      1.1      tnn 			*s->cur &= ~s->mask;
    276      1.1      tnn 		srcmask >>= 1;
    277      1.1      tnn 		s->mask >>= 1;
    278      1.1      tnn 		if (!s->mask) {
    279      1.1      tnn 			s->mask = 0x80;
    280      1.1      tnn 			s->cur++;
    281      1.1      tnn 		}
    282      1.1      tnn 	}
    283      1.1      tnn }
    284      1.1      tnn 
    285      1.1      tnn static void
    286      1.1      tnn ssdfb_bitstream_append_cmd(struct bs_state *s, uint8_t cmd)
    287      1.1      tnn {
    288      1.1      tnn 	ssdfb_bitstream_append(s, 0, 1);
    289      1.1      tnn 	ssdfb_bitstream_append(s, cmd, 0x80);
    290      1.1      tnn }
    291      1.1      tnn 
    292      1.1      tnn static void
    293      1.1      tnn ssdfb_bitstream_append_data(struct bs_state *s, uint8_t *data, size_t len)
    294      1.1      tnn {
    295      1.1      tnn 	while(len--) {
    296      1.1      tnn 		ssdfb_bitstream_append(s, 1, 1);
    297      1.1      tnn 		ssdfb_bitstream_append(s, *data++, 0x80);
    298      1.1      tnn 	}
    299      1.1      tnn }
    300      1.1      tnn 
    301      1.1      tnn static void
    302      1.1      tnn ssdfb_bitstream_final(struct bs_state *s)
    303      1.1      tnn {
    304      1.1      tnn 	uint8_t padding_cmd = SSD1322_CMD_WRITE_RAM;
    305      1.1      tnn 	/* padding_cmd = SSDFB_NOP_CMD; */
    306      1.1      tnn 
    307      1.1      tnn 	while (s->mask != 0x80) {
    308      1.1      tnn 		ssdfb_bitstream_append_cmd(s, padding_cmd);
    309      1.1      tnn 	}
    310      1.1      tnn }
    311      1.1      tnn 
    312      1.1      tnn static void
    313      1.1      tnn ssdfb_spi_4wire_set_dc(struct ssdfb_spi_softc *sc, int value)
    314      1.1      tnn {
    315  1.5.4.2  thorpej 	/* TODO: refactor this if we ever support more that just FDT. */
    316  1.5.4.2  thorpej 
    317  1.5.4.2  thorpej #ifdef FDT
    318  1.5.4.3  thorpej 	KASSERT(sc->sc_gpio_dc != NULL);
    319  1.5.4.2  thorpej 	fdtbus_gpio_write(sc->sc_dc_gpio, value);
    320  1.5.4.2  thorpej #else
    321      1.1      tnn 	/* TODO: this should toggle an auxilliary GPIO pin */
    322      1.1      tnn 	panic("ssdfb_spi_4wire_set_dc");
    323  1.5.4.2  thorpej #endif /* FDT */
    324      1.1      tnn }
    325      1.1      tnn 
    326      1.1      tnn static int
    327      1.1      tnn ssdfb_spi_cmd_4wire(void *cookie, uint8_t *cmd, size_t len, bool usepoll)
    328      1.1      tnn {
    329      1.1      tnn 	struct ssdfb_spi_softc *sc = (struct ssdfb_spi_softc *)cookie;
    330      1.1      tnn 	int error;
    331      1.1      tnn 
    332      1.1      tnn 	ssdfb_spi_4wire_set_dc(sc, 0);
    333      1.1      tnn 	error = spi_send(sc->sc_sh, 1, cmd);
    334      1.1      tnn 	if (error)
    335      1.1      tnn 		return error;
    336      1.1      tnn 	if (len > 1) {
    337      1.1      tnn 		ssdfb_spi_4wire_set_dc(sc, 1);
    338      1.1      tnn 		len--;
    339      1.1      tnn 		cmd++;
    340      1.1      tnn 		error = spi_send(sc->sc_sh, len, cmd);
    341      1.1      tnn 		if (error)
    342      1.1      tnn 			return error;
    343      1.1      tnn 	}
    344      1.1      tnn 
    345      1.1      tnn 	return 0;
    346      1.1      tnn }
    347      1.1      tnn 
    348      1.1      tnn static int
    349      1.1      tnn ssdfb_spi_xfer_rect_4wire_ssd1322(void *cookie, uint8_t fromcol, uint8_t tocol,
    350      1.1      tnn     uint8_t fromrow, uint8_t torow, uint8_t *p, size_t stride, bool usepoll)
    351      1.1      tnn {
    352      1.1      tnn 	struct ssdfb_spi_softc *sc = (struct ssdfb_spi_softc *)cookie;
    353      1.1      tnn 	uint8_t row;
    354      1.1      tnn 	size_t rlen = (tocol + 1 - fromcol) * 2;
    355      1.1      tnn 	int error;
    356      1.1      tnn 	uint8_t cmd;
    357      1.1      tnn 	uint8_t data[2];
    358      1.1      tnn 
    359      1.1      tnn 	/*
    360      1.1      tnn 	 * Unlike iic(4), there is no way to force spi(4) to use polling.
    361      1.1      tnn 	 */
    362      1.2      tnn 	if (usepoll && !cold)
    363      1.1      tnn 		return 0;
    364      1.1      tnn 
    365      1.1      tnn 	ssdfb_spi_4wire_set_dc(sc, 0);
    366      1.1      tnn 	cmd = SSD1322_CMD_SET_ROW_ADDRESS;
    367      1.1      tnn 	error = spi_send(sc->sc_sh, sizeof(cmd), &cmd);
    368      1.1      tnn 	if (error)
    369      1.1      tnn 		return error;
    370      1.1      tnn 	ssdfb_spi_4wire_set_dc(sc, 1);
    371      1.1      tnn 	data[0] = fromrow;
    372      1.1      tnn 	data[1] = torow;
    373      1.1      tnn 	error = spi_send(sc->sc_sh, sizeof(data), data);
    374      1.1      tnn 	if (error)
    375      1.1      tnn 		return error;
    376      1.1      tnn 
    377      1.1      tnn 	ssdfb_spi_4wire_set_dc(sc, 0);
    378      1.1      tnn 	cmd = SSD1322_CMD_SET_COLUMN_ADDRESS;
    379      1.1      tnn 	error = spi_send(sc->sc_sh, sizeof(cmd), &cmd);
    380      1.1      tnn 	if (error)
    381      1.1      tnn 		return error;
    382      1.1      tnn 	ssdfb_spi_4wire_set_dc(sc, 1);
    383      1.1      tnn 	data[0] = fromcol;
    384      1.1      tnn 	data[1] = tocol;
    385      1.1      tnn 	error = spi_send(sc->sc_sh, sizeof(data), data);
    386      1.1      tnn 	if (error)
    387      1.1      tnn 		return error;
    388      1.1      tnn 
    389      1.1      tnn 	ssdfb_spi_4wire_set_dc(sc, 0);
    390      1.1      tnn 	cmd = SSD1322_CMD_WRITE_RAM;
    391      1.1      tnn 	error = spi_send(sc->sc_sh, sizeof(cmd), &cmd);
    392      1.1      tnn 	if (error)
    393      1.1      tnn 		return error;
    394      1.1      tnn 
    395      1.1      tnn 	ssdfb_spi_4wire_set_dc(sc, 1);
    396      1.1      tnn 	for (row = fromrow; row <= torow; row++) {
    397      1.1      tnn 		error = spi_send(sc->sc_sh, rlen, p);
    398      1.1      tnn 		if (error)
    399      1.1      tnn 			return error;
    400      1.1      tnn 		p += stride;
    401      1.1      tnn 	}
    402      1.1      tnn 
    403      1.1      tnn 	return 0;
    404      1.1      tnn }
    405