forked from notro/fbtft
-
Notifications
You must be signed in to change notification settings - Fork 4
/
fb_ssd1322.c
168 lines (140 loc) · 4.75 KB
/
fb_ssd1322.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/init.h>
#include <linux/gpio.h>
#include <linux/spi/spi.h>
#include <linux/delay.h>
#include "fbtft.h"
#define DRVNAME "fb_ssd1322"
#define WIDTH 256
#define HEIGHT 64
#define GAMMA_NUM 1
#define GAMMA_LEN 15
#define DEFAULT_GAMMA "7 1 1 1 1 2 2 3 3 4 4 5 5 6 6"
int init[] = { /* Initialization for LM560G-256064 5.6" OLED display */
-1, 0xFD, 0x12, /* Unlock OLED driver IC */
-1, 0xAE, /* Display OFF (blank) */
-1, 0xB3, 0xF3, /* Display divide clockratio/frequency */
-1, 0xCA, 0x3F, /* Multiplex ratio, 1/64, 64 COMS enabled */
-1, 0xA2, 0x00, /* Set offset, the display map starting line is COM0 */
-1, 0xA1, 0x00, /* Set start line position */
-1, 0xA0, 0x14, 0x11, /* Set remap, horiz address increment, disable colum address remap, */
/* enable nibble remap, scan from com[N-1] to COM0, disable COM split odd even */
-1, 0xAB, 0x01, /* Select external VDD */
-1, 0xB4, 0xA0, 0xFD, /* Display enhancement A, external VSL, enhanced low GS display quality */
-1, 0xC1, 0xFF, /* Contrast current, 256 steps, default is 0x7F */
-1, 0xC7, 0x0F, /* Master contrast current, 16 steps, default is 0x0F */
-1, 0xB1, 0xF0, /* Phase Length */
-1, 0xD1, 0x82, 0x20 /* Display enhancement B */
-1, 0xBB, 0x0D, /* Pre-charge voltage */
-1, 0xBE, 0x00, /* Set VCOMH */
-1, 0xA6, /* Normal display */
-1, 0xAF, /* Display ON */
-3 };
static void set_addr_win(struct fbtft_par *par, int xs, int ys, int xe, int ye)
{
int width = par->info->var.xres;
int offset = (480 - width) / 8;
fbtft_par_dbg(DEBUG_SET_ADDR_WIN, par, "%s(xs=%d, ys=%d, xe=%d, ye=%d)\n", __func__, xs, ys, xe, ye);
write_reg(par, 0x15, offset, offset + (width / 4) - 1);
write_reg(par, 0x75, ys, ye);
write_reg(par, 0x5c);
}
/*
Grayscale Lookup Table
GS1 - GS15
The "Gamma curve" contains the relative values between the entries in the Lookup table.
0 = Setting of GS1 < Setting of GS2 < Setting of GS3..... < Setting of GS14 < Setting of GS15
*/
static int set_gamma(struct fbtft_par *par, unsigned long *curves)
{
unsigned long tmp[GAMMA_LEN * GAMMA_NUM];
int i, acc = 0;
fbtft_par_dbg(DEBUG_INIT_DISPLAY, par, "%s()\n", __func__);
for (i = 0; i < GAMMA_LEN; i++) {
if (i > 0 && curves[i] < 1) {
dev_err(par->info->device,
"Illegal value in Grayscale Lookup Table at index %d. " \
"Must be greater than 0\n", i);
return -EINVAL;
}
acc += curves[i];
tmp[i] = acc;
if (acc > 180) {
dev_err(par->info->device,
"Illegal value(s) in Grayscale Lookup Table. " \
"At index=%d, the accumulated value has exceeded 180\n", i);
return -EINVAL;
}
}
write_reg(par, 0xB8,
tmp[0], tmp[1], tmp[2], tmp[3], tmp[4], tmp[5], tmp[6], tmp[7],
tmp[8], tmp[9], tmp[10], tmp[11], tmp[12], tmp[13], tmp[14]);
return 0;
}
static int blank(struct fbtft_par *par, bool on)
{
fbtft_par_dbg(DEBUG_BLANK, par, "%s(blank=%s)\n", __func__, on ? "true" : "false");
if (on)
write_reg(par, 0xAE);
else
write_reg(par, 0xAF);
return 0;
}
#define CYR 613 /* 2.392 */
#define CYG 601 /* 2.348 */
#define CYB 233 /* 0.912 */
static unsigned int rgb565_to_y(unsigned int rgb)
{
rgb = cpu_to_le16(rgb);
return CYR * (rgb >> 11) + CYG * (rgb >> 5 & 0x3F) + CYB * (rgb & 0x1F);
}
static int write_vmem(struct fbtft_par *par, size_t offset, size_t len)
{
u16 *vmem16 = (u16 *)(par->info->screen_base);
u8 *buf = par->txbuf.buf;
int y, x, bl_height, bl_width;
int ret = 0;
/* Set data line beforehand */
gpio_set_value(par->gpio.dc, 1);
/* convert offset to word index from byte index */
offset /= 2;
bl_width = par->info->var.xres;
bl_height = len / par->info->fix.line_length;
fbtft_par_dbg(DEBUG_WRITE_VMEM, par,
"%s(offset=0x%x bl_width=%d bl_height=%d)\n", __func__, offset, bl_width, bl_height);
for (y = 0; y < bl_height; y++) {
for (x = 0; x < bl_width / 2; x++) {
*buf = cpu_to_le16(rgb565_to_y(vmem16[offset++])) >> 8 & 0xF0;
*buf++ |= cpu_to_le16(rgb565_to_y(vmem16[offset++])) >> 12;
}
}
/* Write data */
ret = par->fbtftops.write(par, par->txbuf.buf, bl_width/2*bl_height);
if (ret < 0)
dev_err(par->info->device, "%s: write failed and returned: %d\n", __func__, ret);
return ret;
}
static struct fbtft_display display = {
.regwidth = 8,
.width = WIDTH,
.height = HEIGHT,
.txbuflen = WIDTH*HEIGHT/2,
.gamma_num = GAMMA_NUM,
.gamma_len = GAMMA_LEN,
.gamma = DEFAULT_GAMMA,
.init_sequence = init,
.fbtftops = {
.write_vmem = write_vmem,
.set_addr_win = set_addr_win,
.blank = blank,
.set_gamma = set_gamma,
},
};
FBTFT_REGISTER_DRIVER(DRVNAME, &display);
MODULE_ALIAS("spi:" DRVNAME);
MODULE_ALIAS("platform:" DRVNAME);
MODULE_DESCRIPTION("SSD1322 OLED Driver");
MODULE_AUTHOR("Ryan Press");
MODULE_LICENSE("GPL");