forked from notro/fbtft
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ssd1351fb.c
269 lines (201 loc) · 5.94 KB
/
ssd1351fb.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/gpio.h>
#include <linux/spi/spi.h>
#include <linux/delay.h>
#include "fbtft.h"
#define DRVNAME "ssd1351fb"
#define WIDTH 128
#define HEIGHT 128
#define GAMMA "5 1 1 1 1 1 1 1 " \
"1 1 1 1 1 1 1 1 " \
"1 1 2 2 1 1 1 2 " \
"2 2 2 2 3 3 3 3 " \
"3 3 3 3 3 3 3 4 " \
"4 4 4 4 4 4 4 4 " \
"4 4 4 5 5 5 5 5 " \
"5 5 5 5 5 5 5"
/* Module Parameter: debug (also available through sysfs) */
MODULE_PARM_DEBUG;
void ssd1351fb_set_addr_win(struct fbtft_par *par, int xs, int ys, int xe, int ye)
{
fbtft_fbtft_dev_dbg(DEBUG_SET_ADDR_WIN, par, par->info->device, "%s(xs=%d, ys=%d, xe=%d, ye=%d)\n", __func__, xs, ys, xe, ye);
write_cmd(par, 0x15);
write_data(par, xs);
write_data(par, xe);
write_cmd(par, 0x75);
write_data(par, ys);
write_data(par, ye);
write_cmd(par, 0x5c);
}
static int ssd1351fb_init_display(struct fbtft_par *par)
{
fbtft_dev_dbg(DEBUG_INIT_DISPLAY, par->info->device, "%s()\n", __func__);
// Reset the device.
par->fbtftops.reset(par);
// Write the init sequence.
write_cmd(par, 0xfd); // Command Lock
write_data(par, 0x12);
write_cmd(par, 0xfd); // Command Lock
write_data(par, 0xb1);
write_cmd(par, 0xae); // Display Off
write_cmd(par, 0xb3); // Front Clock Div
write_data(par, 0xf1);
write_cmd(par, 0xca); // Set Mux Ratio
write_data(par, 0x7f);
write_cmd(par, 0xa0); // Set Colour Depth
write_data(par, 0x74); // 0xb4
write_cmd(par, 0x15); // Set Column Address
write_data(par, 0x00);
write_data(par, 0x7f);
write_cmd(par, 0x75); // Set Row Address
write_data(par, 0x00);
write_data(par, 0x7f);
write_cmd(par, 0xa1); // Set Display Start Line
write_data(par, 0x00);
write_cmd(par, 0xa2); // Set Display Offset
write_data(par, 0x00);
write_cmd(par, 0xb5); // Set GPIO
write_data(par, 0x00);
write_cmd(par, 0xab); // Set Function Selection
write_data(par, 0x01);
write_cmd(par, 0xb1); // Set Phase Length
write_data(par, 0x32);
write_cmd(par, 0xb4); // Set Segment Low Voltage
write_data(par, 0xa0);
write_data(par, 0xb5);
write_data(par, 0x55);
write_cmd(par, 0xbb); // Set Precharge Voltage
write_data(par, 0x17);
write_cmd(par, 0xbe); // Set VComH Voltage
write_data(par, 0x05);
write_cmd(par, 0xc1); // Set Contrast
write_data(par, 0xc8);
write_data(par, 0x80);
write_data(par, 0xc8);
write_cmd(par, 0xc7); // Set Master Contrast
write_data(par, 0x0f);
write_cmd(par, 0xb6); // Set Second Precharge Period
write_data(par, 0x01);
write_cmd(par, 0xa6); // Set Display Mode Reset
write_cmd(par, 0xaf); // Set Sleep Mode Display On
return 0;
}
/*
Grayscale Lookup Table
GS1 - GS63
The "Gamma curve" contains the relative values between the entries in the Lookup table.
From datasheet:
The next 63 data bytes define Gray Scale (GS) Table by
setting the gray scale pulse width in unit of DCLK's
(ranges from 0d ~ 180d)
0 = Setting of GS1 < Setting of GS2 < Setting of GS3..... < Setting of GS62 < Setting of GS63
*/
static int set_gamma(struct fbtft_par *par, unsigned long *curves)
{
int i, acc = 0;
fbtft_dev_dbg(DEBUG_INIT_DISPLAY, par->info->device, "%s()\n", __func__);
/* verify lookup table */
for (i=0;i<63;i++) {
acc += curves[i];
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;
}
if (curves[i] == 0) {
dev_err(par->info->device, "Illegal value in Grayscale Lookup Table. Value can't be zero\n");
return -EINVAL;
}
}
acc = 0;
write_cmd(par, 0xB8);
for (i=0;i<63;i++) {
acc += curves[i];
write_data(par, acc);
}
return 0;
}
static int blank(struct fbtft_par *par, bool on)
{
fbtft_dev_dbg(DEBUG_BLANK, par->info->device, "%s(blank=%s)\n", __func__, on ? "true" : "false");
if (on)
write_cmd(par, 0xAE);
else
write_cmd(par, 0xAF);
return 0;
}
static int ssd1351fb_verify_gpios(struct fbtft_par *par)
{
fbtft_dev_dbg(DEBUG_VERIFY_GPIOS, par->info->device, "%s()\n", __func__);
if (par->gpio.dc < 0)
{
dev_err(par->info->device, "Missing info about 'dc' gpio. Aborting.\n");
return -EINVAL;
}
return 0;
}
struct fbtft_display ssd1351fb_display = {
.width = WIDTH,
.height = HEIGHT,
.gamma_num = 1,
.gamma_len = 63,
.gamma = GAMMA,
};
static int ssd1351fb_probe(struct spi_device *spi)
{
struct fb_info *info;
struct fbtft_par *par;
int ret;
fbtft_dev_dbg(DEBUG_DRIVER_INIT_FUNCTIONS, &spi->dev, "%s()\n", __func__);
info = fbtft_framebuffer_alloc(&ssd1351fb_display, &spi->dev);
if (!info) return -ENOMEM;
par = info->par;
par->spi = spi;
fbtft_debug_init(par);
par->fbtftops.init_display = ssd1351fb_init_display;
par->fbtftops.set_addr_win = ssd1351fb_set_addr_win;
par->fbtftops.verify_gpios = ssd1351fb_verify_gpios;
par->fbtftops.blank = blank;
par->fbtftops.set_gamma = set_gamma;
ret = fbtft_register_framebuffer(info);
if (ret >= 0) return 0;
fbtft_framebuffer_release(info);
return ret;
}
static int ssd1351fb_remove(struct spi_device *spi)
{
struct fb_info *info = spi_get_drvdata(spi);
fbtft_dev_dbg(DEBUG_DRIVER_INIT_FUNCTIONS, &spi->dev, "%s()\n", __func__);
if (info)
{
fbtft_unregister_framebuffer(info);
fbtft_framebuffer_release(info);
}
return 0;
}
static struct spi_driver ssd1351fb_driver =
{
.driver =
{
.name = DRVNAME,
.owner = THIS_MODULE,
},
.probe = ssd1351fb_probe,
.remove = ssd1351fb_remove,
};
static int __init ssd1351fb_init(void)
{
fbtft_pr_debug("\n\n"DRVNAME": %s()\n", __func__);
return spi_register_driver(&ssd1351fb_driver);
}
static void __exit ssd1351fb_exit(void)
{
fbtft_pr_debug(DRVNAME": %s()\n", __func__);
spi_unregister_driver(&ssd1351fb_driver);
}
module_init(ssd1351fb_init);
module_exit(ssd1351fb_exit);
MODULE_DESCRIPTION("SSD1351 OLED Driver");
MODULE_AUTHOR("James Davies");
MODULE_LICENSE("GPL");