Home | History | Annotate | Line # | Download | only in i2c
ssdfb_i2c.c revision 1.3.2.3
      1  1.3.2.3    martin /* $NetBSD: ssdfb_i2c.c,v 1.3.2.3 2020/04/13 08:04:20 martin Exp $ */
      2  1.3.2.2  christos 
      3  1.3.2.2  christos /*
      4  1.3.2.2  christos  * Copyright (c) 2019 The NetBSD Foundation, Inc.
      5  1.3.2.2  christos  * All rights reserved.
      6  1.3.2.2  christos  *
      7  1.3.2.2  christos  * This code is derived from software contributed to The NetBSD Foundation
      8  1.3.2.2  christos  * by Tobias Nygren.
      9  1.3.2.2  christos  *
     10  1.3.2.2  christos  * Redistribution and use in source and binary forms, with or without
     11  1.3.2.2  christos  * modification, are permitted provided that the following conditions
     12  1.3.2.2  christos  * are met:
     13  1.3.2.2  christos  * 1. Redistributions of source code must retain the above copyright
     14  1.3.2.2  christos  *    notice, this list of conditions and the following disclaimer.
     15  1.3.2.2  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.3.2.2  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.3.2.2  christos  *    documentation and/or other materials provided with the distribution.
     18  1.3.2.2  christos  *
     19  1.3.2.2  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.3.2.2  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.3.2.2  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.3.2.2  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.3.2.2  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.3.2.2  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.3.2.2  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.3.2.2  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.3.2.2  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.3.2.2  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.3.2.2  christos  * POSSIBILITY OF SUCH DAMAGE.
     30  1.3.2.2  christos  */
     31  1.3.2.2  christos 
     32  1.3.2.2  christos #include <sys/cdefs.h>
     33  1.3.2.3    martin __KERNEL_RCSID(0, "$NetBSD: ssdfb_i2c.c,v 1.3.2.3 2020/04/13 08:04:20 martin Exp $");
     34  1.3.2.2  christos 
     35  1.3.2.2  christos #include <sys/param.h>
     36  1.3.2.2  christos #include <sys/device.h>
     37  1.3.2.2  christos #include <dev/wscons/wsdisplayvar.h>
     38  1.3.2.2  christos #include <dev/rasops/rasops.h>
     39  1.3.2.2  christos #include <dev/i2c/i2cvar.h>
     40  1.3.2.2  christos #include <dev/ic/ssdfbvar.h>
     41  1.3.2.2  christos 
     42  1.3.2.2  christos struct ssdfb_i2c_softc {
     43  1.3.2.2  christos 	struct		ssdfb_softc sc;
     44  1.3.2.2  christos 	i2c_tag_t	sc_i2c_tag;
     45  1.3.2.2  christos 	i2c_addr_t	sc_i2c_addr;
     46  1.3.2.3    martin 	size_t		sc_transfer_size;
     47  1.3.2.2  christos };
     48  1.3.2.2  christos 
     49  1.3.2.2  christos static int	ssdfb_i2c_match(device_t, cfdata_t, void *);
     50  1.3.2.2  christos static void	ssdfb_i2c_attach(device_t, device_t, void *);
     51  1.3.2.2  christos static int	ssdfb_i2c_detach(device_t, int);
     52  1.3.2.2  christos 
     53  1.3.2.3    martin static int	ssdfb_i2c_probe_transfer_size(struct ssdfb_i2c_softc *, bool);
     54  1.3.2.3    martin static int	ssdfb_i2c_transfer(struct ssdfb_i2c_softc *, uint8_t, uint8_t *,
     55  1.3.2.3    martin 				size_t, int);
     56  1.3.2.2  christos static int	ssdfb_i2c_cmd(void *, uint8_t *, size_t, bool);
     57  1.3.2.2  christos static int	ssdfb_i2c_transfer_rect(void *, uint8_t, uint8_t, uint8_t,
     58  1.3.2.2  christos 				uint8_t, uint8_t *, size_t, bool);
     59  1.3.2.2  christos static int	ssdfb_i2c_transfer_rect_ssd1306(void *, uint8_t, uint8_t,
     60  1.3.2.2  christos 				uint8_t, uint8_t, uint8_t *, size_t, bool);
     61  1.3.2.2  christos static int	ssdfb_i2c_transfer_rect_sh1106(void *, uint8_t, uint8_t,
     62  1.3.2.2  christos 				uint8_t, uint8_t, uint8_t *, size_t, bool);
     63  1.3.2.2  christos static int	ssdfb_smbus_transfer_rect(void *, uint8_t, uint8_t, uint8_t,
     64  1.3.2.2  christos 				uint8_t, uint8_t *, size_t, bool);
     65  1.3.2.2  christos 
     66  1.3.2.2  christos CFATTACH_DECL_NEW(ssdfb_iic, sizeof(struct ssdfb_i2c_softc),
     67  1.3.2.2  christos     ssdfb_i2c_match, ssdfb_i2c_attach, ssdfb_i2c_detach, NULL);
     68  1.3.2.2  christos 
     69  1.3.2.2  christos static const struct device_compatible_entry compat_data[] = {
     70  1.3.2.2  christos 	{ "solomon,ssd1306fb-i2c",	0 },
     71  1.3.2.2  christos 	{ "sino,sh1106fb-i2c",		0 },
     72  1.3.2.2  christos 	{ NULL,				0 }
     73  1.3.2.2  christos };
     74  1.3.2.2  christos 
     75  1.3.2.2  christos static int
     76  1.3.2.2  christos ssdfb_i2c_match(device_t parent, cfdata_t match, void *aux)
     77  1.3.2.2  christos {
     78  1.3.2.2  christos 	struct i2c_attach_args *ia = aux;
     79  1.3.2.2  christos 	int match_result;
     80  1.3.2.2  christos 
     81  1.3.2.2  christos 	if (iic_use_direct_match(ia, match, compat_data, &match_result))
     82  1.3.2.2  christos 		return match_result;
     83  1.3.2.2  christos 
     84  1.3.2.2  christos 	switch (ia->ia_addr) {
     85  1.3.2.2  christos 	case SSDFB_I2C_DEFAULT_ADDR:
     86  1.3.2.2  christos 	case SSDFB_I2C_ALTERNATIVE_ADDR:
     87  1.3.2.2  christos 		return I2C_MATCH_ADDRESS_ONLY;
     88  1.3.2.2  christos 	}
     89  1.3.2.2  christos 
     90  1.3.2.2  christos 	return 0;
     91  1.3.2.2  christos }
     92  1.3.2.2  christos 
     93  1.3.2.2  christos static void
     94  1.3.2.2  christos ssdfb_i2c_attach(device_t parent, device_t self, void *aux)
     95  1.3.2.2  christos {
     96  1.3.2.2  christos 	struct ssdfb_i2c_softc *sc = device_private(self);
     97  1.3.2.2  christos 	struct cfdata *cf = device_cfdata(self);
     98  1.3.2.2  christos 	struct i2c_attach_args *ia = aux;
     99  1.3.2.2  christos 	int flags = cf->cf_flags;
    100  1.3.2.2  christos 	int i;
    101  1.3.2.2  christos 
    102  1.3.2.2  christos 	if ((flags & SSDFB_ATTACH_FLAG_PRODUCT_MASK) == SSDFB_PRODUCT_UNKNOWN) {
    103  1.3.2.2  christos 		for (i = 0; i < ia->ia_ncompat; i++) {
    104  1.3.2.2  christos 			if (strncmp("solomon,ssd1306", ia->ia_compat[i], 15)
    105  1.3.2.2  christos 			    == 0) {
    106  1.3.2.2  christos 				flags |= SSDFB_PRODUCT_SSD1306_GENERIC;
    107  1.3.2.2  christos 				break;
    108  1.3.2.2  christos 			}
    109  1.3.2.2  christos 			else if (strncmp("sino,sh1106", ia->ia_compat[i], 11)
    110  1.3.2.2  christos 			    == 0) {
    111  1.3.2.2  christos 				flags |= SSDFB_PRODUCT_SH1106_GENERIC;
    112  1.3.2.2  christos 				break;
    113  1.3.2.2  christos 			}
    114  1.3.2.2  christos 		}
    115  1.3.2.2  christos 	}
    116  1.3.2.2  christos 	if ((flags & SSDFB_ATTACH_FLAG_PRODUCT_MASK) == SSDFB_PRODUCT_UNKNOWN)
    117  1.3.2.2  christos 		flags |= SSDFB_PRODUCT_SSD1306_GENERIC;
    118  1.3.2.2  christos 
    119  1.3.2.3    martin 	flags |= SSDFB_ATTACH_FLAG_MPSAFE;
    120  1.3.2.2  christos 	sc->sc.sc_dev = self;
    121  1.3.2.2  christos 	sc->sc_i2c_tag = ia->ia_tag;
    122  1.3.2.2  christos 	sc->sc_i2c_addr = ia->ia_addr;
    123  1.3.2.2  christos 	sc->sc.sc_cookie = (void *)sc;
    124  1.3.2.2  christos 	sc->sc.sc_cmd = ssdfb_i2c_cmd;
    125  1.3.2.2  christos 	sc->sc.sc_transfer_rect = ssdfb_i2c_transfer_rect;
    126  1.3.2.2  christos 
    127  1.3.2.2  christos 	ssdfb_attach(&sc->sc, flags);
    128  1.3.2.2  christos }
    129  1.3.2.2  christos 
    130  1.3.2.2  christos static int
    131  1.3.2.2  christos ssdfb_i2c_detach(device_t self, int flags)
    132  1.3.2.2  christos {
    133  1.3.2.2  christos 	struct ssdfb_i2c_softc *sc = device_private(self);
    134  1.3.2.2  christos 
    135  1.3.2.2  christos 	return ssdfb_detach(&sc->sc);
    136  1.3.2.2  christos }
    137  1.3.2.2  christos 
    138  1.3.2.2  christos static int
    139  1.3.2.3    martin ssdfb_i2c_probe_transfer_size(struct ssdfb_i2c_softc *sc, bool usepoll)
    140  1.3.2.3    martin {
    141  1.3.2.3    martin 	int flags = usepoll ? I2C_F_POLL : 0;
    142  1.3.2.3    martin 	uint8_t cb = SSDFB_I2C_CTRL_BYTE_DATA_MASK;
    143  1.3.2.3    martin 	int error;
    144  1.3.2.3    martin 	uint8_t buf[128];
    145  1.3.2.3    martin 	size_t len;
    146  1.3.2.3    martin 
    147  1.3.2.3    martin 	error = iic_acquire_bus(sc->sc_i2c_tag, flags);
    148  1.3.2.3    martin 	if (error)
    149  1.3.2.3    martin 		return error;
    150  1.3.2.3    martin 	len = sizeof(buf);
    151  1.3.2.3    martin 	memset(buf, 0, len);
    152  1.3.2.3    martin 	while (len > 0) {
    153  1.3.2.3    martin 		error = iic_exec(sc->sc_i2c_tag, I2C_OP_WRITE_WITH_STOP,
    154  1.3.2.3    martin 		    sc->sc_i2c_addr, &cb, sizeof(cb), buf, len, flags);
    155  1.3.2.3    martin 		if (!error) {
    156  1.3.2.3    martin 			break;
    157  1.3.2.3    martin 		}
    158  1.3.2.3    martin 		len >>= 1;
    159  1.3.2.3    martin 	}
    160  1.3.2.3    martin 	if (!error && len < 2) {
    161  1.3.2.3    martin 		error = E2BIG;
    162  1.3.2.3    martin 	} else {
    163  1.3.2.3    martin 		sc->sc_transfer_size = len;
    164  1.3.2.3    martin 	}
    165  1.3.2.3    martin 	(void) iic_release_bus(sc->sc_i2c_tag, flags);
    166  1.3.2.3    martin 
    167  1.3.2.3    martin 	return error;
    168  1.3.2.3    martin }
    169  1.3.2.3    martin 
    170  1.3.2.3    martin static int
    171  1.3.2.3    martin ssdfb_i2c_transfer(struct ssdfb_i2c_softc *sc, uint8_t cb, uint8_t *data,
    172  1.3.2.3    martin 		   size_t len, int flags)
    173  1.3.2.3    martin {
    174  1.3.2.3    martin 	int error;
    175  1.3.2.3    martin 	size_t xfer_size = sc->sc_transfer_size;
    176  1.3.2.3    martin 
    177  1.3.2.3    martin 	while (len >= xfer_size) {
    178  1.3.2.3    martin 		error = iic_exec(sc->sc_i2c_tag, I2C_OP_WRITE_WITH_STOP,
    179  1.3.2.3    martin 		    sc->sc_i2c_addr, &cb, sizeof(cb), data, xfer_size, flags);
    180  1.3.2.3    martin 		if (error)
    181  1.3.2.3    martin 			return error;
    182  1.3.2.3    martin 		len -= xfer_size;
    183  1.3.2.3    martin 		data += xfer_size;
    184  1.3.2.3    martin 	}
    185  1.3.2.3    martin 	if (len > 0) {
    186  1.3.2.3    martin 		error = iic_exec(sc->sc_i2c_tag, I2C_OP_WRITE_WITH_STOP,
    187  1.3.2.3    martin 		    sc->sc_i2c_addr, &cb, sizeof(cb), data, len, flags);
    188  1.3.2.3    martin 	}
    189  1.3.2.3    martin 
    190  1.3.2.3    martin 	return error;
    191  1.3.2.3    martin }
    192  1.3.2.3    martin 
    193  1.3.2.3    martin static int
    194  1.3.2.2  christos ssdfb_i2c_cmd(void *cookie, uint8_t *cmd, size_t len, bool usepoll)
    195  1.3.2.2  christos {
    196  1.3.2.2  christos 	struct ssdfb_i2c_softc *sc = (struct ssdfb_i2c_softc *)cookie;
    197  1.3.2.2  christos 	int flags = usepoll ? I2C_F_POLL : 0;
    198  1.3.2.2  christos 	uint8_t cb = 0;
    199  1.3.2.2  christos 	int error;
    200  1.3.2.2  christos 
    201  1.3.2.2  christos 	error = iic_acquire_bus(sc->sc_i2c_tag, flags);
    202  1.3.2.2  christos 	if (error)
    203  1.3.2.2  christos 		return error;
    204  1.3.2.2  christos 	error = iic_exec(sc->sc_i2c_tag, I2C_OP_WRITE_WITH_STOP,
    205  1.3.2.2  christos 	    sc->sc_i2c_addr, &cb, sizeof(cb), cmd, len, flags);
    206  1.3.2.2  christos 	(void) iic_release_bus(sc->sc_i2c_tag, flags);
    207  1.3.2.2  christos 
    208  1.3.2.2  christos 	return error;
    209  1.3.2.2  christos }
    210  1.3.2.2  christos 
    211  1.3.2.2  christos static int
    212  1.3.2.2  christos ssdfb_i2c_transfer_rect(void *cookie, uint8_t fromcol, uint8_t tocol,
    213  1.3.2.2  christos     uint8_t frompage, uint8_t topage, uint8_t *p, size_t stride, bool usepoll)
    214  1.3.2.2  christos {
    215  1.3.2.2  christos 	struct ssdfb_i2c_softc *sc = (struct ssdfb_i2c_softc *)cookie;
    216  1.3.2.2  christos 	uint8_t cmd[2];
    217  1.3.2.2  christos 	int error;
    218  1.3.2.2  christos 
    219  1.3.2.2  christos 	/*
    220  1.3.2.2  christos 	 * Test if large transfers are supported by the parent i2c bus and
    221  1.3.2.2  christos 	 * pick the fastest transfer routine for subsequent invocations.
    222  1.3.2.2  christos 	 */
    223  1.3.2.2  christos 	switch (sc->sc.sc_p->p_controller_id) {
    224  1.3.2.2  christos 	case SSDFB_CONTROLLER_SSD1306:
    225  1.3.2.2  christos 		sc->sc.sc_transfer_rect = ssdfb_i2c_transfer_rect_ssd1306;
    226  1.3.2.2  christos 		break;
    227  1.3.2.2  christos 	case SSDFB_CONTROLLER_SH1106:
    228  1.3.2.2  christos 		sc->sc.sc_transfer_rect = ssdfb_i2c_transfer_rect_sh1106;
    229  1.3.2.2  christos 		break;
    230  1.3.2.2  christos 	default:
    231  1.3.2.2  christos 		sc->sc.sc_transfer_rect = ssdfb_smbus_transfer_rect;
    232  1.3.2.2  christos 		break;
    233  1.3.2.2  christos 	}
    234  1.3.2.2  christos 
    235  1.3.2.2  christos 	if (sc->sc.sc_transfer_rect != ssdfb_smbus_transfer_rect) {
    236  1.3.2.3    martin 		error = ssdfb_i2c_probe_transfer_size(sc, usepoll);
    237  1.3.2.2  christos 		if (error)
    238  1.3.2.2  christos 			return error;
    239  1.3.2.3    martin 		aprint_verbose_dev(sc->sc.sc_dev, "%zd-byte transfers\n",
    240  1.3.2.3    martin 		    sc->sc_transfer_size);
    241  1.3.2.3    martin 		if (sc->sc_transfer_size == 2) {
    242  1.3.2.2  christos 			sc->sc.sc_transfer_rect = ssdfb_smbus_transfer_rect;
    243  1.3.2.2  christos 		}
    244  1.3.2.2  christos 	}
    245  1.3.2.2  christos 
    246  1.3.2.2  christos 	/*
    247  1.3.2.2  christos 	 * Set addressing mode for SSD1306.
    248  1.3.2.2  christos 	 */
    249  1.3.2.2  christos 	if (sc->sc.sc_p->p_controller_id == SSDFB_CONTROLLER_SSD1306) {
    250  1.3.2.2  christos 		cmd[0] = SSD1306_CMD_SET_MEMORY_ADDRESSING_MODE;
    251  1.3.2.2  christos 		cmd[1] = sc->sc.sc_transfer_rect
    252  1.3.2.2  christos 		    == ssdfb_i2c_transfer_rect_ssd1306
    253  1.3.2.2  christos 		    ? SSD1306_MEMORY_ADDRESSING_MODE_HORIZONTAL
    254  1.3.2.2  christos 		    : SSD1306_MEMORY_ADDRESSING_MODE_PAGE;
    255  1.3.2.2  christos 		error = ssdfb_i2c_cmd(cookie, cmd, sizeof(cmd), usepoll);
    256  1.3.2.2  christos 		if (error)
    257  1.3.2.2  christos 			return error;
    258  1.3.2.2  christos 	}
    259  1.3.2.2  christos 
    260  1.3.2.2  christos 	return sc->sc.sc_transfer_rect(cookie, fromcol, tocol, frompage, topage,
    261  1.3.2.2  christos 	    p, stride, usepoll);
    262  1.3.2.2  christos }
    263  1.3.2.2  christos 
    264  1.3.2.2  christos 
    265  1.3.2.2  christos static int
    266  1.3.2.2  christos ssdfb_i2c_transfer_rect_ssd1306(void *cookie, uint8_t fromcol, uint8_t tocol,
    267  1.3.2.2  christos     uint8_t frompage, uint8_t topage, uint8_t *p, size_t stride, bool usepoll)
    268  1.3.2.2  christos {
    269  1.3.2.2  christos 	struct ssdfb_i2c_softc *sc = (struct ssdfb_i2c_softc *)cookie;
    270  1.3.2.2  christos 	int flags = usepoll ? I2C_F_POLL : 0;
    271  1.3.2.2  christos 	uint8_t cc = 0;
    272  1.3.2.2  christos 	uint8_t cb = SSDFB_I2C_CTRL_BYTE_DATA_MASK;
    273  1.3.2.2  christos 	size_t len = tocol + 1 - fromcol;
    274  1.3.2.2  christos 	int error;
    275  1.3.2.2  christos 	/*
    276  1.3.2.2  christos 	 * SSD1306 does not implement the Continuation bit correctly.
    277  1.3.2.2  christos 	 * The SH1106 protocol defines that a control byte WITH Co
    278  1.3.2.2  christos 	 * set must be inserted between each command. But SSD1306
    279  1.3.2.2  christos 	 * fails to parse the commands if we do that.
    280  1.3.2.2  christos 	 */
    281  1.3.2.2  christos 	uint8_t cmds[] = {
    282  1.3.2.2  christos 		SSD1306_CMD_SET_COLUMN_ADDRESS,
    283  1.3.2.2  christos 		fromcol, tocol,
    284  1.3.2.2  christos 		SSD1306_CMD_SET_PAGE_ADDRESS,
    285  1.3.2.2  christos 		frompage, topage
    286  1.3.2.2  christos 	};
    287  1.3.2.2  christos 
    288  1.3.2.2  christos 	error = iic_acquire_bus(sc->sc_i2c_tag, flags);
    289  1.3.2.2  christos 	if (error)
    290  1.3.2.2  christos 		return error;
    291  1.3.2.2  christos 	error = iic_exec(sc->sc_i2c_tag, I2C_OP_WRITE_WITH_STOP,
    292  1.3.2.2  christos 	    sc->sc_i2c_addr, &cc, sizeof(cc), cmds, sizeof(cmds), flags);
    293  1.3.2.2  christos 	if (error)
    294  1.3.2.2  christos 		goto out;
    295  1.3.2.2  christos 	while (frompage <= topage) {
    296  1.3.2.3    martin 		error = ssdfb_i2c_transfer(sc, cb, p, len, flags);
    297  1.3.2.2  christos 		if (error)
    298  1.3.2.2  christos 			goto out;
    299  1.3.2.2  christos 		frompage++;
    300  1.3.2.2  christos 		p += stride;
    301  1.3.2.2  christos 	}
    302  1.3.2.2  christos out:
    303  1.3.2.2  christos 	(void) iic_release_bus(sc->sc_i2c_tag, flags);
    304  1.3.2.2  christos 
    305  1.3.2.2  christos 	return error;
    306  1.3.2.2  christos }
    307  1.3.2.2  christos 
    308  1.3.2.2  christos static int
    309  1.3.2.2  christos ssdfb_i2c_transfer_rect_sh1106(void *cookie, uint8_t fromcol, uint8_t tocol,
    310  1.3.2.2  christos     uint8_t frompage, uint8_t topage, uint8_t *p, size_t stride, bool usepoll)
    311  1.3.2.2  christos {
    312  1.3.2.2  christos 	struct ssdfb_i2c_softc *sc = (struct ssdfb_i2c_softc *)cookie;
    313  1.3.2.2  christos 	int flags = usepoll ? I2C_F_POLL : 0;
    314  1.3.2.2  christos 	uint8_t cb = SSDFB_I2C_CTRL_BYTE_DATA_MASK;
    315  1.3.2.2  christos 	uint8_t cc = SSDFB_I2C_CTRL_BYTE_CONTINUATION_MASK;
    316  1.3.2.2  christos 	size_t len = tocol + 1 - fromcol;
    317  1.3.2.2  christos 	int error;
    318  1.3.2.2  christos 	uint8_t cmds[] = {
    319  1.3.2.2  christos 		SSDFB_CMD_SET_PAGE_START_ADDRESS_BASE + frompage,
    320  1.3.2.2  christos 		SSDFB_I2C_CTRL_BYTE_CONTINUATION_MASK,
    321  1.3.2.2  christos 		SSDFB_CMD_SET_HIGHER_COLUMN_START_ADDRESS_BASE + (fromcol >> 4),
    322  1.3.2.2  christos 		SSDFB_I2C_CTRL_BYTE_CONTINUATION_MASK,
    323  1.3.2.2  christos 		SSDFB_CMD_SET_LOWER_COLUMN_START_ADDRESS_BASE + (fromcol & 0xf)
    324  1.3.2.2  christos 	};
    325  1.3.2.2  christos 
    326  1.3.2.2  christos 	error = iic_acquire_bus(sc->sc_i2c_tag, flags);
    327  1.3.2.2  christos 	if (error)
    328  1.3.2.2  christos 		return error;
    329  1.3.2.2  christos 	while (frompage <= topage) {
    330  1.3.2.2  christos 		cmds[0] = SSDFB_CMD_SET_PAGE_START_ADDRESS_BASE + frompage;
    331  1.3.2.2  christos 		error = iic_exec(sc->sc_i2c_tag, I2C_OP_WRITE_WITH_STOP,
    332  1.3.2.2  christos 		    sc->sc_i2c_addr, &cc, sizeof(cc), cmds, sizeof(cmds), flags);
    333  1.3.2.2  christos 		if (error)
    334  1.3.2.2  christos 			goto out;
    335  1.3.2.3    martin 		error = ssdfb_i2c_transfer(sc, cb, p, len, flags);
    336  1.3.2.2  christos 		if (error)
    337  1.3.2.2  christos 			goto out;
    338  1.3.2.2  christos 		frompage++;
    339  1.3.2.2  christos 		p += stride;
    340  1.3.2.2  christos 	}
    341  1.3.2.2  christos out:
    342  1.3.2.2  christos 	(void) iic_release_bus(sc->sc_i2c_tag, flags);
    343  1.3.2.2  christos 
    344  1.3.2.2  christos 	return error;
    345  1.3.2.2  christos }
    346  1.3.2.2  christos 
    347  1.3.2.2  christos /*
    348  1.3.2.2  christos  * If the parent is an SMBus, then we can only send 2 bytes
    349  1.3.2.2  christos  * of payload per txn. The SSD1306 triple byte commands are
    350  1.3.2.2  christos  * not available so we have to use PAGE addressing mode
    351  1.3.2.2  christos  * and split data into multiple txns.
    352  1.3.2.2  christos  * This is ugly and slow but it's the best we can do.
    353  1.3.2.2  christos  */
    354  1.3.2.2  christos static int
    355  1.3.2.2  christos ssdfb_smbus_transfer_rect(void *cookie, uint8_t fromcol, uint8_t tocol,
    356  1.3.2.2  christos     uint8_t frompage, uint8_t topage, uint8_t *p, size_t stride, bool usepoll)
    357  1.3.2.2  christos {
    358  1.3.2.2  christos 	struct ssdfb_i2c_softc *sc = (struct ssdfb_i2c_softc *)cookie;
    359  1.3.2.2  christos 	int flags = usepoll ? I2C_F_POLL : 0;
    360  1.3.2.2  christos 	uint8_t cb = SSDFB_I2C_CTRL_BYTE_DATA_MASK;
    361  1.3.2.2  christos 	uint8_t cc = 0;
    362  1.3.2.2  christos 	size_t len = tocol + 1 - fromcol;
    363  1.3.2.2  christos 	uint8_t cmd_higher_col =
    364  1.3.2.2  christos 	    SSDFB_CMD_SET_HIGHER_COLUMN_START_ADDRESS_BASE + (fromcol >> 4);
    365  1.3.2.2  christos 	uint8_t cmd_lower_col =
    366  1.3.2.2  christos 	    SSDFB_CMD_SET_LOWER_COLUMN_START_ADDRESS_BASE + (fromcol & 0xf);
    367  1.3.2.2  christos 	uint8_t cmd_page;
    368  1.3.2.2  christos 	uint8_t data[2];
    369  1.3.2.2  christos 	uint8_t *colp;
    370  1.3.2.2  christos 	uint8_t *endp;
    371  1.3.2.2  christos 	int error;
    372  1.3.2.2  christos 
    373  1.3.2.2  christos 	error = iic_acquire_bus(sc->sc_i2c_tag, flags);
    374  1.3.2.2  christos 	if (error)
    375  1.3.2.2  christos 		return error;
    376  1.3.2.2  christos 	while (frompage <= topage) {
    377  1.3.2.2  christos 		error = iic_exec(sc->sc_i2c_tag, I2C_OP_WRITE_WITH_STOP,
    378  1.3.2.2  christos 		    sc->sc_i2c_addr, &cc, sizeof(cc),
    379  1.3.2.2  christos 		    &cmd_higher_col, sizeof(cmd_higher_col), flags);
    380  1.3.2.2  christos 		if (error)
    381  1.3.2.2  christos 			goto out;
    382  1.3.2.2  christos 		error = iic_exec(sc->sc_i2c_tag, I2C_OP_WRITE_WITH_STOP,
    383  1.3.2.2  christos 		    sc->sc_i2c_addr, &cc, sizeof(cc),
    384  1.3.2.2  christos 		    &cmd_lower_col, sizeof(cmd_lower_col), flags);
    385  1.3.2.2  christos 		if (error)
    386  1.3.2.2  christos 			goto out;
    387  1.3.2.2  christos 		cmd_page = SSDFB_CMD_SET_PAGE_START_ADDRESS_BASE + frompage;
    388  1.3.2.2  christos 		error = iic_exec(sc->sc_i2c_tag, I2C_OP_WRITE_WITH_STOP,
    389  1.3.2.2  christos 		    sc->sc_i2c_addr, &cc, sizeof(cc),
    390  1.3.2.2  christos 		    &cmd_page, sizeof(cmd_page), flags);
    391  1.3.2.2  christos 		if (error)
    392  1.3.2.2  christos 			goto out;
    393  1.3.2.2  christos 		colp = p;
    394  1.3.2.2  christos 		endp = colp + len;
    395  1.3.2.2  christos 		if (len & 1) {
    396  1.3.2.2  christos 			data[0] = *colp++;
    397  1.3.2.2  christos 			error = iic_exec(sc->sc_i2c_tag, I2C_OP_WRITE_WITH_STOP,
    398  1.3.2.2  christos 			    sc->sc_i2c_addr, &cb, sizeof(cb), data, 1, flags);
    399  1.3.2.2  christos 			if (error)
    400  1.3.2.2  christos 				goto out;
    401  1.3.2.2  christos 		}
    402  1.3.2.2  christos 		while (colp < endp) {
    403  1.3.2.2  christos 			/*
    404  1.3.2.2  christos 			 * Send two bytes at a time. We can't use colp directly
    405  1.3.2.2  christos 			 * because i2c controllers sometimes have data alignment
    406  1.3.2.2  christos 			 * requirements.
    407  1.3.2.2  christos 			 */
    408  1.3.2.2  christos 			data[0] = *colp++;
    409  1.3.2.2  christos 			data[1] = *colp++;
    410  1.3.2.2  christos 			error = iic_exec(sc->sc_i2c_tag, I2C_OP_WRITE_WITH_STOP,
    411  1.3.2.2  christos 			    sc->sc_i2c_addr, &cb, sizeof(cb), data, 2, flags);
    412  1.3.2.2  christos 			if (error)
    413  1.3.2.2  christos 				goto out;
    414  1.3.2.2  christos 		}
    415  1.3.2.2  christos 		frompage++;
    416  1.3.2.2  christos 		p += stride;
    417  1.3.2.2  christos 	}
    418  1.3.2.2  christos out:
    419  1.3.2.2  christos 	(void) iic_release_bus(sc->sc_i2c_tag, flags);
    420  1.3.2.3    martin 
    421  1.3.2.2  christos 	return error;
    422  1.3.2.2  christos }
    423