Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev/i2c add i2c attachment for ssdfb



details:   https://anonhg.NetBSD.org/src/rev/eb7d2e06e61f
branches:  trunk
changeset: 449691:eb7d2e06e61f
user:      tnn <tnn%NetBSD.org@localhost>
date:      Sun Mar 17 01:03:47 2019 +0000

description:
add i2c attachment for ssdfb

diffstat:

 sys/dev/i2c/files.i2c   |    6 +-
 sys/dev/i2c/ssdfb_i2c.c |  368 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 373 insertions(+), 1 deletions(-)

diffs (truncated from 392 to 300 lines):

diff -r e98012cb42a8 -r eb7d2e06e61f sys/dev/i2c/files.i2c
--- a/sys/dev/i2c/files.i2c     Sun Mar 17 00:57:15 2019 +0000
+++ b/sys/dev/i2c/files.i2c     Sun Mar 17 01:03:47 2019 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: files.i2c,v 1.98 2019/02/03 13:17:12 jmcneill Exp $
+#      $NetBSD: files.i2c,v 1.99 2019/03/17 01:03:47 tnn Exp $
 
 obsolete defflag       opt_i2cbus.h            I2C_SCAN
 define i2cbus { }
@@ -358,6 +358,10 @@
 attach rkreg at rkpmic
 file   dev/i2c/rkpmic.c                        rkpmic
 
+# SSD1306 or SH1106 OLED/PLED display
+attach ssdfb at iic with ssdfb_iic
+file   dev/i2c/ssdfb_i2c.c                     ssdfb_iic
+
 # Analogix ANX6345 eDP transmitter
 device anxedp: edid, videomode, drmkms, drmkms_i2c
 attach anxedp at iic
diff -r e98012cb42a8 -r eb7d2e06e61f sys/dev/i2c/ssdfb_i2c.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/dev/i2c/ssdfb_i2c.c   Sun Mar 17 01:03:47 2019 +0000
@@ -0,0 +1,368 @@
+/* $NetBSD: ssdfb_i2c.c,v 1.1 2019/03/17 01:03:47 tnn Exp $ */
+
+/*
+ * Copyright (c) 2019 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Tobias Nygren.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: ssdfb_i2c.c,v 1.1 2019/03/17 01:03:47 tnn Exp $");
+
+#include <sys/param.h>
+#include <sys/device.h>
+#include <dev/wscons/wsdisplayvar.h>
+#include <dev/rasops/rasops.h>
+#include <dev/i2c/i2cvar.h>
+#include <dev/ic/ssdfbvar.h>
+
+struct ssdfb_i2c_softc {
+       struct          ssdfb_softc sc;
+       i2c_tag_t       sc_i2c_tag;
+       i2c_addr_t      sc_i2c_addr;
+       bool            sc_parent_is_smbus;
+};
+
+static int     ssdfb_i2c_match(device_t, cfdata_t, void *);
+static void    ssdfb_i2c_attach(device_t, device_t, void *);
+static int     ssdfb_i2c_detach(device_t, int);
+
+static int     ssdfb_i2c_cmd(void *, uint8_t *, size_t, bool);
+static int     ssdfb_i2c_transfer_rect(void *, uint8_t, uint8_t, uint8_t,
+                               uint8_t, uint8_t *, size_t, bool);
+static int     ssdfb_i2c_transfer_rect_ssd1306(void *, uint8_t, uint8_t,
+                               uint8_t, uint8_t, uint8_t *, size_t, bool);
+static int     ssdfb_i2c_transfer_rect_sh1106(void *, uint8_t, uint8_t,
+                               uint8_t, uint8_t, uint8_t *, size_t, bool);
+static int     ssdfb_smbus_transfer_rect(void *, uint8_t, uint8_t, uint8_t,
+                               uint8_t, uint8_t *, size_t, bool);
+
+CFATTACH_DECL_NEW(ssdfb_iic, sizeof(struct ssdfb_i2c_softc),
+    ssdfb_i2c_match, ssdfb_i2c_attach, ssdfb_i2c_detach, NULL);
+
+static const struct device_compatible_entry compat_data[] = {
+       { "solomon,ssd1306fb-i2c",      0 },
+       { "sino,sh1106fb-i2c",          0 },
+       { NULL,                         0 }
+};
+
+static int
+ssdfb_i2c_match(device_t parent, cfdata_t match, void *aux)
+{
+       struct i2c_attach_args *ia = aux;
+       int match_result;
+
+       if (iic_use_direct_match(ia, match, compat_data, &match_result))
+               return match_result;
+
+       switch (ia->ia_addr) {
+       case SSDFB_I2C_DEFAULT_ADDR:
+       case SSDFB_I2C_ALTERNATIVE_ADDR:
+               return I2C_MATCH_ADDRESS_ONLY;
+       }
+
+       return 0;
+}
+
+static void
+ssdfb_i2c_attach(device_t parent, device_t self, void *aux)
+{
+       struct ssdfb_i2c_softc *sc = device_private(self);
+       struct cfdata *cf = device_cfdata(self);
+       struct i2c_attach_args *ia = aux;
+       int flags = cf->cf_flags;
+       int i;
+
+       if ((flags & SSDFB_ATTACH_FLAG_PRODUCT_MASK) == SSDFB_PRODUCT_UNKNOWN) {
+               for (i = 0; i < ia->ia_ncompat; i++) {
+                       if (strncmp("solomon,ssd1306", ia->ia_compat[i], 15)
+                           == 0) {
+                               flags |= SSDFB_PRODUCT_SSD1306_GENERIC;
+                               break;
+                       }
+                       else if (strncmp("sino,sh1106", ia->ia_compat[i], 11)
+                           == 0) {
+                               flags |= SSDFB_PRODUCT_SH1106_GENERIC;
+                               break;
+                       }
+               }
+       }
+       if ((flags & SSDFB_ATTACH_FLAG_PRODUCT_MASK) == SSDFB_PRODUCT_UNKNOWN)
+               flags |= SSDFB_PRODUCT_SSD1306_GENERIC;
+
+       sc->sc.sc_dev = self;
+       sc->sc_i2c_tag = ia->ia_tag;
+       sc->sc_i2c_addr = ia->ia_addr;
+       sc->sc.sc_cookie = (void *)sc;
+       sc->sc.sc_cmd = ssdfb_i2c_cmd;
+       sc->sc.sc_transfer_rect = ssdfb_i2c_transfer_rect;
+
+       ssdfb_attach(&sc->sc, flags);
+}
+
+static int
+ssdfb_i2c_detach(device_t self, int flags)
+{
+       struct ssdfb_i2c_softc *sc = device_private(self);
+
+       return ssdfb_detach(&sc->sc);
+}
+
+static int
+ssdfb_i2c_cmd(void *cookie, uint8_t *cmd, size_t len, bool usepoll)
+{
+       struct ssdfb_i2c_softc *sc = (struct ssdfb_i2c_softc *)cookie;
+       int flags = usepoll ? I2C_F_POLL : 0;
+       uint8_t cb = 0;
+       int error;
+
+       error = iic_acquire_bus(sc->sc_i2c_tag, flags);
+       if (error)
+               return error;
+       error = iic_exec(sc->sc_i2c_tag, I2C_OP_WRITE_WITH_STOP,
+           sc->sc_i2c_addr, &cb, sizeof(cb), cmd, len, flags);
+       (void) iic_release_bus(sc->sc_i2c_tag, flags);
+
+       return error;
+}
+
+static int
+ssdfb_i2c_transfer_rect(void *cookie, uint8_t fromcol, uint8_t tocol,
+    uint8_t frompage, uint8_t topage, uint8_t *p, size_t stride, bool usepoll)
+{
+       struct ssdfb_i2c_softc *sc = (struct ssdfb_i2c_softc *)cookie;
+       int flags = usepoll ? I2C_F_POLL : 0;
+       uint8_t cb = SSDFB_I2C_CTRL_BYTE_DATA_MASK;
+       uint8_t data [] = {0, 0, 0};
+       uint8_t cmd[2];
+       int error;
+
+       /*
+        * Test if large transfers are supported by the parent i2c bus and
+        * pick the fastest transfer routine for subsequent invocations.
+        */
+       switch (sc->sc.sc_p->p_controller_id) {
+       case SSDFB_CONTROLLER_SSD1306:
+               sc->sc.sc_transfer_rect = ssdfb_i2c_transfer_rect_ssd1306;
+               break;
+       case SSDFB_CONTROLLER_SH1106:
+               sc->sc.sc_transfer_rect = ssdfb_i2c_transfer_rect_sh1106;
+               break;
+       default:
+               sc->sc.sc_transfer_rect = ssdfb_smbus_transfer_rect;
+               break;
+       }
+
+       if (sc->sc.sc_transfer_rect != ssdfb_smbus_transfer_rect) {
+               error = iic_acquire_bus(sc->sc_i2c_tag, flags);
+               if (error)
+                       return error;
+               error = iic_exec(sc->sc_i2c_tag, I2C_OP_WRITE_WITH_STOP,
+                   sc->sc_i2c_addr, &cb, sizeof(cb), data, sizeof(data), flags);
+               (void) iic_release_bus(sc->sc_i2c_tag, flags);
+               if (error) {
+                       sc->sc.sc_transfer_rect = ssdfb_smbus_transfer_rect;
+               }
+       }
+
+       /*
+        * Set addressing mode for SSD1306.
+        */
+       if (sc->sc.sc_p->p_controller_id == SSDFB_CONTROLLER_SSD1306) {
+               cmd[0] = SSD1306_CMD_SET_MEMORY_ADDRESSING_MODE;
+               cmd[1] = sc->sc.sc_transfer_rect
+                   == ssdfb_i2c_transfer_rect_ssd1306
+                   ? SSD1306_MEMORY_ADDRESSING_MODE_HORIZONTAL
+                   : SSD1306_MEMORY_ADDRESSING_MODE_PAGE;
+               error = ssdfb_i2c_cmd(cookie, cmd, sizeof(cmd), usepoll);
+               if (error)
+                       return error;
+       }
+
+       return sc->sc.sc_transfer_rect(cookie, fromcol, tocol, frompage, topage,
+           p, stride, usepoll);
+}
+
+
+static int
+ssdfb_i2c_transfer_rect_ssd1306(void *cookie, uint8_t fromcol, uint8_t tocol,
+    uint8_t frompage, uint8_t topage, uint8_t *p, size_t stride, bool usepoll)
+{
+       struct ssdfb_i2c_softc *sc = (struct ssdfb_i2c_softc *)cookie;
+       int flags = usepoll ? I2C_F_POLL : 0;
+       uint8_t cc = 0;
+       uint8_t cb = SSDFB_I2C_CTRL_BYTE_DATA_MASK;
+       size_t len = (tocol + 1 - fromcol);
+       int error;
+       /*
+        * SSD1306 does not implement the Continuation bit correctly.
+        * The SH1106 protocol defines that a control byte WITH Co
+        * set must be inserted between each command. But SSD1306
+        * fails to parse the commands if we do that.
+        */
+       uint8_t cmds[] = {
+               SSD1306_CMD_SET_COLUMN_ADDRESS,
+               fromcol, tocol,
+               SSD1306_CMD_SET_PAGE_ADDRESS,
+               frompage, topage
+       };
+
+       error = iic_acquire_bus(sc->sc_i2c_tag, flags);
+       if (error)
+               return error;
+       error = iic_exec(sc->sc_i2c_tag, I2C_OP_WRITE_WITH_STOP,
+           sc->sc_i2c_addr, &cc, sizeof(cc), cmds, sizeof(cmds), flags);
+       if (error)
+               goto out;
+       while (frompage <= topage) {
+               error = iic_exec(sc->sc_i2c_tag, I2C_OP_WRITE_WITH_STOP,
+                   sc->sc_i2c_addr, &cb, sizeof(cb), p, len, flags);
+               if (error)
+                       goto out;
+               frompage++;
+               p += stride;
+       }
+out:
+       (void) iic_release_bus(sc->sc_i2c_tag, flags);
+
+       return error;
+}
+
+static int
+ssdfb_i2c_transfer_rect_sh1106(void *cookie, uint8_t fromcol, uint8_t tocol,
+    uint8_t frompage, uint8_t topage, uint8_t *p, size_t stride, bool usepoll)
+{
+       struct ssdfb_i2c_softc *sc = (struct ssdfb_i2c_softc *)cookie;
+       int flags = usepoll ? I2C_F_POLL : 0;
+       uint8_t cb = SSDFB_I2C_CTRL_BYTE_DATA_MASK;
+       uint8_t cc = SSDFB_I2C_CTRL_BYTE_CONTINUATION_MASK;
+       size_t len = (tocol + 1 - fromcol);
+       int error;
+       uint8_t cmds[] = {
+               SSDFB_CMD_SET_PAGE_START_ADDRESS_BASE + frompage,
+               SSDFB_I2C_CTRL_BYTE_CONTINUATION_MASK,
+               SSDFB_CMD_SET_HIGHER_COLUMN_START_ADDRESS_BASE + (fromcol >> 4),
+               SSDFB_I2C_CTRL_BYTE_CONTINUATION_MASK,
+               SSDFB_CMD_SET_LOWER_COLUMN_START_ADDRESS_BASE + (fromcol & 0xf)
+       };
+
+       error = iic_acquire_bus(sc->sc_i2c_tag, flags);
+       if (error)
+               return error;
+       while (frompage <= topage) {
+               cmds[0] = SSDFB_CMD_SET_PAGE_START_ADDRESS_BASE + frompage;



Home | Main Index | Thread Index | Old Index