1 /*
2  * FB driver for the SH1106 OLED Controller
3  * Based on the SSD1306 driver by Noralf Tronnes
4  *
5  * Copyright (C) 2017 Heiner Kallweit
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17 
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/gpio.h>
22 #include <linux/delay.h>
23 
24 #include "fbtft.h"
25 
26 #define DRVNAME		"fb_sh1106"
27 #define WIDTH		128
28 #define HEIGHT		64
29 
30 /* Init sequence based on the Adafruit SSD1306 Arduino library */
31 static int init_display(struct fbtft_par *par)
32 {
33 	if (!par->info->var.xres || par->info->var.xres > WIDTH ||
34 	    !par->info->var.yres || par->info->var.yres > HEIGHT ||
35 	    par->info->var.yres % 8) {
36 		dev_err(par->info->device, "Invalid screen size\n");
37 		return -EINVAL;
38 	}
39 
40 	if (par->info->var.rotate) {
41 		dev_err(par->info->device, "Display rotation not supported\n");
42 		return -EINVAL;
43 	}
44 
45 	par->fbtftops.reset(par);
46 
47 	/* Set Display OFF */
48 	write_reg(par, 0xAE);
49 
50 	/* Set Display Clock Divide Ratio/ Oscillator Frequency */
51 	write_reg(par, 0xD5, 0x80);
52 
53 	/* Set Multiplex Ratio */
54 	write_reg(par, 0xA8, par->info->var.yres - 1);
55 
56 	/* Set Display Offset */
57 	write_reg(par, 0xD3, 0x00);
58 
59 	/* Set Display Start Line */
60 	write_reg(par, 0x40 | 0x0);
61 
62 	/* Set Segment Re-map */
63 	/* column address 127 is mapped to SEG0 */
64 	write_reg(par, 0xA0 | 0x1);
65 
66 	/* Set COM Output Scan Direction */
67 	/* remapped mode. Scan from COM[N-1] to COM0 */
68 	write_reg(par, 0xC8);
69 
70 	/* Set COM Pins Hardware Configuration */
71 	if (par->info->var.yres == 64)
72 		/* A[4]=1b, Alternative COM pin configuration */
73 		write_reg(par, 0xDA, 0x12);
74 	else if (par->info->var.yres == 48)
75 		/* A[4]=1b, Alternative COM pin configuration */
76 		write_reg(par, 0xDA, 0x12);
77 	else
78 		/* A[4]=0b, Sequential COM pin configuration */
79 		write_reg(par, 0xDA, 0x02);
80 
81 	/* Set Pre-charge Period */
82 	write_reg(par, 0xD9, 0xF1);
83 
84 	/* Set VCOMH Deselect Level */
85 	write_reg(par, 0xDB, 0x40);
86 
87 	/* Set Display ON */
88 	write_reg(par, 0xAF);
89 
90 	msleep(150);
91 
92 	return 0;
93 }
94 
95 static void set_addr_win(struct fbtft_par *par, int xs, int ys, int xe, int ye)
96 {
97 }
98 
99 static int blank(struct fbtft_par *par, bool on)
100 {
101 	fbtft_par_dbg(DEBUG_BLANK, par, "%s(blank=%s)\n",
102 		      __func__, on ? "true" : "false");
103 
104 	write_reg(par, on ? 0xAE : 0xAF);
105 
106 	return 0;
107 }
108 
109 /* Gamma is used to control Contrast */
110 static int set_gamma(struct fbtft_par *par, u32 *curves)
111 {
112 	/* apply mask */
113 	curves[0] &= 0xFF;
114 
115 	/* Set Contrast Control for BANK0 */
116 	write_reg(par, 0x81, curves[0]);
117 
118 	return 0;
119 }
120 
121 static int write_vmem(struct fbtft_par *par, size_t offset, size_t len)
122 {
123 	u16 *vmem16 = (u16 *)par->info->screen_buffer;
124 	u32 xres = par->info->var.xres;
125 	int page, page_start, page_end, x, i, ret;
126 	u8 *buf = par->txbuf.buf;
127 
128 	/* offset refers to vmem with 2 bytes element size */
129 	page_start = offset / (8 * 2 * xres);
130 	page_end = DIV_ROUND_UP(offset + len, 8 * 2 * xres);
131 
132 	for (page = page_start; page < page_end; page++) {
133 		/* set page and set column to 2 because of vidmem width 132 */
134 		write_reg(par, 0xb0 | page, 0x00 | 2, 0x10 | 0);
135 
136 		memset(buf, 0, xres);
137 		for (x = 0; x < xres; x++)
138 			for (i = 0; i < 8; i++)
139 				if (vmem16[(page * 8 + i) * xres + x])
140 					buf[x] |= BIT(i);
141 
142 		/* Write data */
143 		ret = fbtft_write_buf_dc(par, buf, xres, 1);
144 		if (ret < 0)
145 			return ret;
146 	}
147 
148 	return 0;
149 }
150 
151 static void write_register(struct fbtft_par *par, int len, ...)
152 {
153 	va_list args;
154 	int i;
155 
156 	va_start(args, len);
157 
158 	for (i = 0; i < len; i++)
159 		par->buf[i] = va_arg(args, unsigned int);
160 
161 	/* keep DC low for all command bytes to transfer */
162 	fbtft_write_buf_dc(par, par->buf, len, 0);
163 
164 	va_end(args);
165 }
166 
167 static struct fbtft_display display = {
168 	.regwidth = 8,
169 	.width = WIDTH,
170 	.height = HEIGHT,
171 	.txbuflen = WIDTH,
172 	.gamma_num = 1,
173 	.gamma_len = 1,
174 	/* set default contrast to 0xcd = 80% */
175 	.gamma = "cd",
176 	.fbtftops = {
177 		.write_vmem = write_vmem,
178 		.write_register = write_register,
179 		.init_display = init_display,
180 		.set_addr_win = set_addr_win,
181 		.blank = blank,
182 		.set_gamma = set_gamma,
183 	},
184 };
185 
186 FBTFT_REGISTER_DRIVER(DRVNAME, "sinowealth,sh1106", &display);
187 
188 MODULE_ALIAS("spi:" DRVNAME);
189 MODULE_ALIAS("platform:" DRVNAME);
190 MODULE_ALIAS("spi:sh1106");
191 MODULE_ALIAS("platform:sh1106");
192 
193 MODULE_DESCRIPTION("SH1106 OLED Driver");
194 MODULE_AUTHOR("Heiner Kallweit");
195 MODULE_LICENSE("GPL");
196