#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/gpio/consumer.h>
#include <linux/delay.h>
#include "fbtft.h"
#define DRVNAME "fb_ssd1306"
#define WIDTH 128
#define HEIGHT 64
static int init_display(struct fbtft_par *par)
{
par->fbtftops.reset(par);
if (par->gamma.curves[0] == 0) {
mutex_lock(&par->gamma.lock);
if (par->info->var.yres == 64)
par->gamma.curves[0] = 0xCF;
else
par->gamma.curves[0] = 0x8F;
mutex_unlock(&par->gamma.lock);
}
write_reg(par, 0xAE);
write_reg(par, 0xD5);
write_reg(par, 0x80);
write_reg(par, 0xA8);
if (par->info->var.yres == 64)
write_reg(par, 0x3F);
else if (par->info->var.yres == 48)
write_reg(par, 0x2F);
else
write_reg(par, 0x1F);
write_reg(par, 0xD3);
write_reg(par, 0x0);
write_reg(par, 0x40 | 0x0);
write_reg(par, 0x8D);
write_reg(par, 0x14);
write_reg(par, 0x20);
write_reg(par, 0x01);
write_reg(par, 0xA0 | 0x1);
write_reg(par, 0xC8);
write_reg(par, 0xDA);
if (par->info->var.yres == 64)
write_reg(par, 0x12);
else if (par->info->var.yres == 48)
write_reg(par, 0x12);
else
write_reg(par, 0x02);
write_reg(par, 0xD9);
write_reg(par, 0xF1);
write_reg(par, 0xDB);
write_reg(par, 0x40);
write_reg(par, 0xA4);
write_reg(par, 0xA6);
write_reg(par, 0xAF);
return 0;
}
static void set_addr_win_64x48(struct fbtft_par *par)
{
write_reg(par, 0x21);
write_reg(par, 0x20);
write_reg(par, 0x5F);
write_reg(par, 0x22);
write_reg(par, 0x0);
write_reg(par, 0x5);
}
static void set_addr_win(struct fbtft_par *par, int xs, int ys, int xe, int ye)
{
write_reg(par, 0x00 | 0x0);
write_reg(par, 0x10 | 0x0);
write_reg(par, 0x40 | 0x0);
if (par->info->var.xres == 64 && par->info->var.yres == 48)
set_addr_win_64x48(par);
}
static int blank(struct fbtft_par *par, bool on)
{
fbtft_par_dbg(DEBUG_BLANK, par, "(%s=%s)\n",
__func__, on ? "true" : "false");
if (on)
write_reg(par, 0xAE);
else
write_reg(par, 0xAF);
return 0;
}
static int set_gamma(struct fbtft_par *par, u32 *curves)
{
curves[0] &= 0xFF;
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;
u32 yres = par->info->var.yres;
u8 *buf = par->txbuf.buf;
int x, y, i;
int ret = 0;
for (x = 0; x < xres; x++) {
for (y = 0; y < yres / 8; y++) {
*buf = 0x00;
for (i = 0; i < 8; i++)
if (vmem16[(y * 8 + i) * xres + x])
*buf |= BIT(i);
buf++;
}
}
gpiod_set_value(par->gpio.dc, 1);
ret = par->fbtftops.write(par, par->txbuf.buf, xres * yres / 8);
if (ret < 0)
dev_err(par->info->device, "write failed and returned: %d\n",
ret);
return ret;
}
static struct fbtft_display display = {
.regwidth = 8,
.width = WIDTH,
.height = HEIGHT,
.gamma_num = 1,
.gamma_len = 1,
.gamma = "00",
.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, "solomon,ssd1306", &display);
MODULE_ALIAS("spi:" DRVNAME);
MODULE_ALIAS("platform:" DRVNAME);
MODULE_ALIAS("spi:ssd1306");
MODULE_ALIAS("platform:ssd1306");
MODULE_DESCRIPTION("SSD1306 OLED Driver");
MODULE_AUTHOR("Noralf Tronnes");
MODULE_LICENSE("GPL"