Source-Changes-HG archive

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

[src/trunk]: src/sys/dev/ic add ssdfb



details:   https://anonhg.NetBSD.org/src/rev/f757c195788a
branches:  trunk
changeset: 997626:f757c195788a
user:      tnn <tnn%NetBSD.org@localhost>
date:      Sun Mar 17 00:57:15 2019 +0000

description:
add ssdfb

The ssdfb driver provides wsdisplay(4) support for OLED/PLED framebuffer
modules based on one of the following controller chips:

- Solomon Systech Ltd SSD1306
- Sino Wealth Electronic Ltd SH1106

It supports xf86-video-wsfb and can optionally attach as the console.

Some products that should work with this driver include:
- Adafruit 0.96" 128x64 OLED graphic display
- Adafruit 1.3" 128x64 OLED graphic display
- Adafruit 128x32 OLED graphic display
- Adafruit PiOLED 128x32 Monochome OLED for Raspberry Pi
- "GM009605" commonly distributed with Arduino starter kits
- display modules made by Chengdu Heltec Automation technology co. LTD

Note: I used the name ssdfb(4) because that's what OpenBSD calls their
driver but the implementation is entirely unique to NetBSD.

diffstat:

 sys/dev/ic/ssdfb.c    |  1001 +++++++++++++++++++++++++++++++++++++++++++++++++
 sys/dev/ic/ssdfbvar.h |   237 +++++++++++
 2 files changed, 1238 insertions(+), 0 deletions(-)

diffs (truncated from 1246 to 300 lines):

diff -r fab056638f29 -r f757c195788a sys/dev/ic/ssdfb.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/dev/ic/ssdfb.c        Sun Mar 17 00:57:15 2019 +0000
@@ -0,0 +1,1001 @@
+/* $NetBSD: ssdfb.c,v 1.1 2019/03/17 00:57:15 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.c,v 1.1 2019/03/17 00:57:15 tnn Exp $");
+
+#include "opt_ddb.h"
+
+#include <sys/param.h>
+#include <sys/kernel.h>
+#include <uvm/uvm_page.h>
+#include <sys/condvar.h>
+#include <sys/kmem.h>
+#include <sys/kthread.h>
+#include <dev/wscons/wsdisplayvar.h>
+#include <dev/rasops/rasops.h>
+#include <dev/ic/ssdfbvar.h>
+
+#if defined(DDB)
+#include <machine/db_machdep.h>
+#include <ddb/db_extern.h>
+#endif
+
+/* userland interface */
+static int     ssdfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
+static paddr_t ssdfb_mmap(void *, void *, off_t, int);
+
+/* wscons screen management */
+static int     ssdfb_alloc_screen(void *, const struct wsscreen_descr *,
+                                  void **, int *, int *, long *);
+static void    ssdfb_free_screen(void *, void *);
+static int     ssdfb_show_screen(void *, void *, int,
+                                 void (*cb) (void *, int, int), void *);
+
+/* rasops hooks */
+static void    ssdfb_putchar(void *, int, int, u_int, long);
+static void    ssdfb_copycols(void *, int, int, int, int);
+static void    ssdfb_erasecols(void *, int, int, int, long);
+static void    ssdfb_copyrows(void *, int, int, int);
+static void    ssdfb_eraserows(void *, int, int, long);
+static void    ssdfb_cursor(void *, int, int, int);
+
+/* hardware interface */
+static int     ssdfb_init(struct ssdfb_softc *);
+static int     ssdfb_set_contrast(struct ssdfb_softc *, uint8_t, bool);
+static int     ssdfb_set_display_on(struct ssdfb_softc *, bool, bool);
+static int     ssdfb_set_mode(struct ssdfb_softc *, u_int);
+
+/* frame buffer damage tracking and synchronization */
+static bool    ssdfb_is_modified(struct ssdfb_softc *sc);
+static bool    ssdfb_clear_modify(struct ssdfb_softc *sc);
+static void    ssdfb_damage(struct ssdfb_softc *);
+static void    ssdfb_thread(void *);
+static void    ssdfb_set_usepoll(struct ssdfb_softc *, bool);
+static int     ssdfb_sync(struct ssdfb_softc *, bool);
+static uint64_t        ssdfb_transpose_block_1bpp(uint8_t *, size_t);
+static uint64_t        ssdfb_transpose_block_8bpp(uint8_t *, size_t);
+
+/* misc helpers */
+static const struct ssdfb_product *
+               ssdfb_lookup_product(ssdfb_product_id_t);
+static int     ssdfb_pick_font(int *, struct wsdisplay_font **);
+static void    ssdfb_clear_screen(struct ssdfb_softc *);
+#if defined(DDB)
+static void    ssdfb_ddb_trap_callback(int);
+#endif
+
+static const char *ssdfb_controller_names[] = {
+       [SSDFB_CONTROLLER_UNKNOWN] =    "unknown",
+       [SSDFB_CONTROLLER_SSD1306] =    "Solomon Systech SSD1306",
+       [SSDFB_CONTROLLER_SH1106] =     "Sino Wealth SH1106"
+};
+
+/*
+ * Display module assemblies supported by this driver.
+ */
+static const struct ssdfb_product ssdfb_products[] = {
+       {
+               .p_product_id =         SSDFB_PRODUCT_SSD1306_GENERIC,
+               .p_controller_id =      SSDFB_CONTROLLER_SSD1306,
+               .p_name =               "generic",
+               .p_width =              128,
+               .p_height =             64,
+               .p_panel_shift =        0,
+               .p_fosc =               0x8,
+               .p_fosc_div =           0,
+               .p_precharge =          0x1,
+               .p_discharge =          0xf,
+               .p_compin_cfg =         SSDFB_COM_PINS_A1_MASK
+                                       | SSDFB_COM_PINS_ALTERNATIVE_MASK,
+               .p_vcomh_deselect_level = SSD1306_VCOMH_DESELECT_LEVEL_0_77_VCC,
+               .p_default_contrast =   0x7f,
+               .p_multiplex_ratio =    0x3f,
+               .p_chargepump_cmd =     SSD1306_CMD_SET_CHARGE_PUMP,
+               .p_chargepump_arg =     SSD1306_CHARGE_PUMP_ENABLE
+       },
+       {
+               .p_product_id =         SSDFB_PRODUCT_SH1106_GENERIC,
+               .p_controller_id =      SSDFB_CONTROLLER_SH1106,
+               .p_name =               "generic",
+               .p_width =              128,
+               .p_height =             64,
+               .p_panel_shift =        2,
+               .p_fosc =               0x5,
+               .p_fosc_div =           0,
+               .p_precharge =          0x2,
+               .p_discharge =          0x2,
+               .p_compin_cfg =         SSDFB_COM_PINS_A1_MASK
+                                       | SSDFB_COM_PINS_ALTERNATIVE_MASK,
+               .p_vcomh_deselect_level = SH1106_VCOMH_DESELECT_LEVEL_DEFAULT,
+               .p_default_contrast =   0x80,
+               .p_multiplex_ratio =    0x3f,
+               .p_chargepump_cmd =     SH1106_CMD_SET_CHARGE_PUMP_7V4,
+               .p_chargepump_arg =     SSDFB_CMD_NOP
+       },
+       {
+               .p_product_id =         SSDFB_PRODUCT_ADAFRUIT_938,
+               .p_controller_id =      SSDFB_CONTROLLER_SSD1306,
+               .p_name =               "Adafruit Industries, LLC product 938",
+               .p_width =              128,
+               .p_height =             64,
+               .p_panel_shift =        0,
+               .p_fosc =               0x8,
+               .p_fosc_div =           0,
+               .p_precharge =          0x1,
+               .p_discharge =          0xf,
+               .p_compin_cfg =         0x12,
+               .p_vcomh_deselect_level = 0x40,
+               .p_default_contrast =   0x8f,
+               .p_multiplex_ratio =    0x3f,
+               .p_chargepump_cmd =     SSD1306_CMD_SET_CHARGE_PUMP,
+               .p_chargepump_arg =     SSD1306_CHARGE_PUMP_ENABLE
+       },
+       {
+               .p_product_id =         SSDFB_PRODUCT_ADAFRUIT_931,
+               .p_controller_id =      SSDFB_CONTROLLER_SSD1306,
+               .p_name =               "Adafruit Industries, LLC product 931",
+               .p_width =              128,
+               .p_height =             32,
+               .p_panel_shift =        0,
+               .p_fosc =               0x8,
+               .p_fosc_div =           0,
+               .p_precharge =          0x1,
+               .p_discharge =          0xf,
+               .p_compin_cfg =         0x2,
+               .p_vcomh_deselect_level = 0x40,
+               .p_default_contrast =   0x8f,
+               .p_multiplex_ratio =    0x1f,
+               .p_chargepump_cmd =     SSD1306_CMD_SET_CHARGE_PUMP,
+               .p_chargepump_arg =     SSD1306_CHARGE_PUMP_ENABLE
+       }
+};
+
+static const struct wsdisplay_accessops ssdfb_accessops = {
+       .ioctl =        ssdfb_ioctl,
+       .mmap =         ssdfb_mmap,
+       .alloc_screen = ssdfb_alloc_screen,
+       .free_screen =  ssdfb_free_screen,
+       .show_screen =  ssdfb_show_screen
+};
+
+#define SSDFB_CMD1(c) do { cmd[0] = (c); error = sc->sc_cmd(sc->sc_cookie, cmd, 1, usepoll); } while(0)
+#define SSDFB_CMD2(c, a) do { cmd[0] = (c); cmd[1] = (a); error = sc->sc_cmd(sc->sc_cookie, cmd, 2, usepoll); } while(0)
+#define SSDFB_CMD3(c, a, b) do { cmd[0] = (c); cmd[1] = (a); cmd[2] = (b); error = sc->sc_cmd(sc->sc_cookie, cmd, 3, usepoll); } while(0)
+
+void
+ssdfb_attach(struct ssdfb_softc *sc, int flags)
+{
+       struct wsemuldisplaydev_attach_args aa;
+       struct rasops_info *ri = &sc->sc_ri;
+       int error = 0;
+       long defattr;
+       const struct ssdfb_product *p;
+
+       p = ssdfb_lookup_product(flags & SSDFB_ATTACH_FLAG_PRODUCT_MASK);
+       if (p == NULL) {
+               aprint_error(": unknown display assembly\n");
+               return;
+       }
+       sc->sc_p = p;
+
+       aprint_naive("\n");
+       aprint_normal(": %s (%s)\n",
+           ssdfb_controller_names[p->p_controller_id],
+           p->p_name);
+
+       sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
+       sc->sc_is_console = flags & SSDFB_ATTACH_FLAG_CONSOLE ? true : false;
+       sc->sc_inverse = flags & SSDFB_ATTACH_FLAG_INVERSE ? true : false;
+       sc->sc_upsidedown = flags & SSDFB_ATTACH_FLAG_UPSIDEDOWN ? true : false;
+       sc->sc_backoff = 1;
+       sc->sc_contrast = sc->sc_p->p_default_contrast;
+       sc->sc_gddram_len = sc->sc_p->p_width * sc->sc_p->p_height / 8;
+       sc->sc_gddram = kmem_alloc(sc->sc_gddram_len, KM_SLEEP);
+       if (sc->sc_gddram == NULL)
+               goto out;
+
+       aprint_normal_dev(sc->sc_dev, "%dx%d%s\n", sc->sc_p->p_width,
+           sc->sc_p->p_height, sc->sc_is_console ? ", console" : "");
+
+       /*
+        * Initialize rasops. The native depth is 1-bit monochrome and we
+        * support this in text emul mode via rasops1. But modern Xorg
+        * userland has many rendering glitches when running with 1-bit depth
+        * so to better support this use case we instead declare ourselves as
+        * an 8-bit display with a two entry constant color map.
+        */
+       error = ssdfb_pick_font(&sc->sc_fontcookie, &sc->sc_font);
+       if (error) {
+               aprint_error_dev(sc->sc_dev, "no font\n");
+               goto out;
+       }
+       ri->ri_depth =  8;
+       ri->ri_font =   sc->sc_font;
+       ri->ri_width =  sc->sc_p->p_width;
+       ri->ri_height = sc->sc_p->p_height;
+       ri->ri_stride = ri->ri_width * ri->ri_depth / 8;
+       ri->ri_hw =     sc;
+       ri->ri_flg =    RI_FULLCLEAR;
+       sc->sc_ri_bits_len = round_page(ri->ri_stride * ri->ri_height);
+       ri->ri_bits     = (u_char *)uvm_km_alloc(kernel_map, sc->sc_ri_bits_len,
+                                                0, UVM_KMF_WIRED);
+       if (ri->ri_bits == NULL)
+               goto out;
+
+       error = rasops_init(ri,
+           sc->sc_p->p_height / sc->sc_font->fontheight,
+           sc->sc_p->p_width  / sc->sc_font->fontwidth);
+       if (error)
+               goto out;
+
+       ri->ri_caps &= ~WSSCREEN_WSCOLORS;
+
+       /*
+        * Save original emul ops & insert our damage notification hooks.
+        */
+       sc->sc_orig_riops =     ri->ri_ops;
+       ri->ri_ops.putchar =    ssdfb_putchar;
+       ri->ri_ops.copycols =   ssdfb_copycols;
+       ri->ri_ops.erasecols =  ssdfb_erasecols;
+       ri->ri_ops.copyrows =   ssdfb_copyrows;
+       ri->ri_ops.eraserows =  ssdfb_eraserows;
+       ri->ri_ops.cursor =     ssdfb_cursor;
+
+       /*
+        * Set up the screen.
+        */
+       sc->sc_screen_descr = (struct wsscreen_descr){
+               .name =         "default",
+               .ncols =        ri->ri_cols,
+               .nrows =        ri->ri_rows,
+               .textops =      &ri->ri_ops,
+               .fontwidth =    ri->ri_font->fontwidth,
+               .fontheight =   ri->ri_font->fontheight,
+               .capabilities = ri->ri_caps
+       };
+       sc->sc_screens[0] = &sc->sc_screen_descr;
+       sc->sc_screenlist = (struct wsscreen_list){
+               .nscreens =     1,
+               .screens =      sc->sc_screens
+       };
+
+       /*
+        * Initialize hardware.
+        */
+       error = ssdfb_init(sc);
+       if (error)
+               goto out;



Home | Main Index | Thread Index | Old Index