staging: fbtft: add adriver for SH1106
authorHeiner Kallweit <hkallweit1@gmail.com>
Tue, 28 Feb 2017 07:10:38 +0000 (08:10 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Mon, 6 Mar 2017 08:17:08 +0000 (09:17 +0100)
This patch adds support for the SH1106 OLED controller chip.

It's similar to the SSD1306, however it supports only a subset of
commands and the internal video memory has a different width.

Major differences:
- Internal video memory has a width of 132 pixels
- Only a subset of commands is supported
- Only page addressing mode is supported, no support for vertical and
  horizontal addressing
- SPI frequency limited to 2 MHz according to the datasheet

Successfully tested with a 1.3" OLED display with this chip and
SPI interface.

Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/staging/fbtft/Kconfig
drivers/staging/fbtft/Makefile
drivers/staging/fbtft/fb_sh1106.c [new file with mode: 0644]

index 6f5e824..dba6767 100644 (file)
@@ -111,6 +111,12 @@ config FB_TFT_S6D1121
        help
          Generic Framebuffer support for S6D1121
 
+config FB_TFT_SH1106
+       tristate "FB driver for the SH1106 OLED Controller"
+       depends on FB_TFT
+       help
+         Framebuffer support for SH1106
+
 config FB_TFT_SSD1289
        tristate "FB driver for the SSD1289 LCD Controller"
        depends on FB_TFT
index 2725ea9..05ae9fb 100644 (file)
@@ -20,6 +20,7 @@ obj-$(CONFIG_FB_TFT_PCD8544)     += fb_pcd8544.o
 obj-$(CONFIG_FB_TFT_RA8875)      += fb_ra8875.o
 obj-$(CONFIG_FB_TFT_S6D02A1)     += fb_s6d02a1.o
 obj-$(CONFIG_FB_TFT_S6D1121)     += fb_s6d1121.o
+obj-$(CONFIG_FB_TFT_SH1106)      += fb_sh1106.o
 obj-$(CONFIG_FB_TFT_SSD1289)     += fb_ssd1289.o
 obj-$(CONFIG_FB_TFT_SSD1305)     += fb_ssd1305.o
 obj-$(CONFIG_FB_TFT_SSD1306)     += fb_ssd1306.o
diff --git a/drivers/staging/fbtft/fb_sh1106.c b/drivers/staging/fbtft/fb_sh1106.c
new file mode 100644 (file)
index 0000000..b7896e5
--- /dev/null
@@ -0,0 +1,203 @@
+/*
+ * FB driver for the SH1106 OLED Controller
+ * Based on the SSD1306 driver by Noralf Tronnes
+ *
+ * Copyright (C) 2017 Heiner Kallweit
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/init.h>
+#include <linux/gpio.h>
+#include <linux/delay.h>
+
+#include "fbtft.h"
+
+#define DRVNAME                "fb_sh1106"
+#define WIDTH          128
+#define HEIGHT         64
+
+/*
+ * write_reg() caveat:
+ *
+ * This doesn't work because D/C has to be LOW for both values:
+ * write_reg(par, val1, val2);
+ *
+ * Do it like this:
+ * write_reg(par, val1);
+ * write_reg(par, val2);
+ */
+
+/* Init sequence based on the Adafruit SSD1306 Arduino library */
+static int init_display(struct fbtft_par *par)
+{
+       if (!par->info->var.xres || par->info->var.xres > WIDTH ||
+           !par->info->var.yres || par->info->var.yres > HEIGHT ||
+           par->info->var.yres % 8) {
+               dev_err(par->info->device, "Invalid screen size\n");
+               return -EINVAL;
+       }
+
+       if (par->info->var.rotate) {
+               dev_err(par->info->device, "Display rotation not supported\n");
+               return -EINVAL;
+       }
+
+       par->fbtftops.reset(par);
+
+       /* Set Display OFF */
+       write_reg(par, 0xAE);
+
+       /* Set Display Clock Divide Ratio/ Oscillator Frequency */
+       write_reg(par, 0xD5);
+       write_reg(par, 0x80);
+
+       /* Set Multiplex Ratio */
+       write_reg(par, 0xA8);
+       write_reg(par, par->info->var.yres - 1);
+
+       /* Set Display Offset */
+       write_reg(par, 0xD3);
+       write_reg(par, 0x0);
+
+       /* Set Display Start Line */
+       write_reg(par, 0x40 | 0x0);
+
+       /* Set Segment Re-map */
+       /* column address 127 is mapped to SEG0 */
+       write_reg(par, 0xA0 | 0x1);
+
+       /* Set COM Output Scan Direction */
+       /* remapped mode. Scan from COM[N-1] to COM0 */
+       write_reg(par, 0xC8);
+
+       /* Set COM Pins Hardware Configuration */
+       write_reg(par, 0xDA);
+       if (par->info->var.yres == 64)
+               /* A[4]=1b, Alternative COM pin configuration */
+               write_reg(par, 0x12);
+       else if (par->info->var.yres == 48)
+               /* A[4]=1b, Alternative COM pin configuration */
+               write_reg(par, 0x12);
+       else
+               /* A[4]=0b, Sequential COM pin configuration */
+               write_reg(par, 0x02);
+
+       /* Set Pre-charge Period */
+       write_reg(par, 0xD9);
+       write_reg(par, 0xF1);
+
+       /* Set VCOMH Deselect Level */
+       write_reg(par, 0xDB);
+       write_reg(par, 0x40);
+
+       /* Set Display ON */
+       write_reg(par, 0xAF);
+
+       msleep(150);
+
+       return 0;
+}
+
+static void set_addr_win(struct fbtft_par *par, int xs, int ys, int xe, int ye)
+{
+}
+
+static int blank(struct fbtft_par *par, bool on)
+{
+       fbtft_par_dbg(DEBUG_BLANK, par, "%s(blank=%s)\n",
+                     __func__, on ? "true" : "false");
+
+       write_reg(par, on ? 0xAE : 0xAF);
+
+       return 0;
+}
+
+/* Gamma is used to control Contrast */
+static int set_gamma(struct fbtft_par *par, u32 *curves)
+{
+       /* apply mask */
+       curves[0] &= 0xFF;
+
+       /* Set Contrast Control for BANK0 */
+       write_reg(par, 0x81);
+       write_reg(par, curves[0]);
+
+       return 0;
+}
+
+static int write_vmem(struct fbtft_par *par, size_t offset, size_t len)
+{
+       u16 *vmem16 = (u16 *)par->info->screen_buffer;
+       u32 xres = par->info->var.xres;
+       int page, page_start, page_end, x, i, ret;
+       u8 *buf = par->txbuf.buf;
+
+       /* offset refers to vmem with 2 bytes element size */
+       page_start = offset / (8 * 2 * xres);
+       page_end = DIV_ROUND_UP(offset + len, 8 * 2 * xres);
+
+       for (page = page_start; page < page_end; page++) {
+               /* set page */
+               write_reg(par, 0xb0 | page);
+               /* set column to 2 to compensate for vidmem width 132 */
+               write_reg(par, 0x00 | 2);
+               write_reg(par, 0x10 | 0);
+
+               memset(buf, 0, xres);
+               for (x = 0; x < xres; x++)
+                       for (i = 0; i < 8; i++)
+                               if (vmem16[(page * 8 + i) * xres + x])
+                                       buf[x] |= BIT(i);
+
+               /* Write data */
+               gpio_set_value(par->gpio.dc, 1);
+               ret = par->fbtftops.write(par, buf, xres);
+               if (ret < 0) {
+                       dev_err(par->info->device,
+                               "write failed and returned: %d\n", ret);
+                       return ret;
+               }
+       }
+
+       return 0;
+}
+
+static struct fbtft_display display = {
+       .regwidth = 8,
+       .width = WIDTH,
+       .height = HEIGHT,
+       .txbuflen = WIDTH,
+       .gamma_num = 1,
+       .gamma_len = 1,
+       /* set default contrast to 0xcd = 80% */
+       .gamma = "cd",
+       .fbtftops = {
+               .write_vmem = write_vmem,
+               .init_display = init_display,
+               .set_addr_win = set_addr_win,
+               .blank = blank,
+               .set_gamma = set_gamma,
+       },
+};
+
+FBTFT_REGISTER_DRIVER(DRVNAME, "sinowealth,sh1106", &display);
+
+MODULE_ALIAS("spi:" DRVNAME);
+MODULE_ALIAS("platform:" DRVNAME);
+MODULE_ALIAS("spi:sh1106");
+MODULE_ALIAS("platform:sh1106");
+
+MODULE_DESCRIPTION("SH1106 OLED Driver");
+MODULE_AUTHOR("Heiner Kallweit");
+MODULE_LICENSE("GPL");