1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <linux/bitops.h>
18#include <linux/delay.h>
19#include <linux/init.h>
20#include <linux/kernel.h>
21#include <linux/module.h>
22#include <video/mipi_display.h>
23
24#include "fbtft.h"
25
26#define DRVNAME "fb_st7789v"
27
28#define DEFAULT_GAMMA \
29 "70 2C 2E 15 10 09 48 33 53 0B 19 18 20 25\n" \
30 "70 2C 2E 15 10 09 48 33 53 0B 19 18 20 25"
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54enum st7789v_command {
55 PORCTRL = 0xB2,
56 GCTRL = 0xB7,
57 VCOMS = 0xBB,
58 VDVVRHEN = 0xC2,
59 VRHS = 0xC3,
60 VDVS = 0xC4,
61 VCMOFSET = 0xC5,
62 PWCTRL1 = 0xD0,
63 PVGAMCTRL = 0xE0,
64 NVGAMCTRL = 0xE1,
65};
66
67#define MADCTL_BGR BIT(3)
68#define MADCTL_MV BIT(5)
69#define MADCTL_MX BIT(6)
70#define MADCTL_MY BIT(7)
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86static int init_display(struct fbtft_par *par)
87{
88
89 write_reg(par, MIPI_DCS_EXIT_SLEEP_MODE);
90 mdelay(120);
91
92
93 write_reg(par, MIPI_DCS_SET_PIXEL_FORMAT, MIPI_DCS_PIXEL_FMT_16BIT);
94
95 write_reg(par, PORCTRL, 0x08, 0x08, 0x00, 0x22, 0x22);
96
97
98
99
100
101 write_reg(par, GCTRL, 0x35);
102
103
104
105
106
107 write_reg(par, VDVVRHEN, 0x01, 0xFF);
108
109
110
111
112
113 write_reg(par, VRHS, 0x0B);
114
115
116 write_reg(par, VDVS, 0x20);
117
118
119 write_reg(par, VCOMS, 0x20);
120
121
122 write_reg(par, VCMOFSET, 0x20);
123
124
125
126
127
128
129 write_reg(par, PWCTRL1, 0xA4, 0xA1);
130
131 write_reg(par, MIPI_DCS_SET_DISPLAY_ON);
132 return 0;
133}
134
135
136
137
138
139
140
141
142static int set_var(struct fbtft_par *par)
143{
144 u8 madctl_par = 0;
145
146 if (par->bgr)
147 madctl_par |= MADCTL_BGR;
148 switch (par->info->var.rotate) {
149 case 0:
150 break;
151 case 90:
152 madctl_par |= (MADCTL_MV | MADCTL_MY);
153 break;
154 case 180:
155 madctl_par |= (MADCTL_MX | MADCTL_MY);
156 break;
157 case 270:
158 madctl_par |= (MADCTL_MV | MADCTL_MX);
159 break;
160 default:
161 return -EINVAL;
162 }
163 write_reg(par, MIPI_DCS_SET_ADDRESS_MODE, madctl_par);
164 return 0;
165}
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181static int set_gamma(struct fbtft_par *par, unsigned long *curves)
182{
183 int i;
184 int j;
185 int c;
186
187
188
189
190
191
192 const u8 gamma_par_mask[] = {
193 0xFF,
194 0x3F,
195 0x3F,
196 0x1F,
197 0x1F,
198 0x3F,
199 0x7F,
200 0x77,
201 0x7F,
202 0x3F,
203 0x1F,
204 0x1F,
205 0x3F,
206 0x3F,
207 };
208
209 for (i = 0; i < par->gamma.num_curves; i++) {
210 c = i * par->gamma.num_values;
211 for (j = 0; j < par->gamma.num_values; j++)
212 curves[c + j] &= gamma_par_mask[j];
213 write_reg(
214 par, PVGAMCTRL + i,
215 curves[c + 0], curves[c + 1], curves[c + 2],
216 curves[c + 3], curves[c + 4], curves[c + 5],
217 curves[c + 6], curves[c + 7], curves[c + 8],
218 curves[c + 9], curves[c + 10], curves[c + 11],
219 curves[c + 12], curves[c + 13]);
220 }
221 return 0;
222}
223
224
225
226
227
228
229
230
231
232static int blank(struct fbtft_par *par, bool on)
233{
234 if (on)
235 write_reg(par, MIPI_DCS_SET_DISPLAY_OFF);
236 else
237 write_reg(par, MIPI_DCS_SET_DISPLAY_ON);
238 return 0;
239}
240
241static struct fbtft_display display = {
242 .regwidth = 8,
243 .width = 240,
244 .height = 320,
245 .gamma_num = 2,
246 .gamma_len = 14,
247 .gamma = DEFAULT_GAMMA,
248 .fbtftops = {
249 .init_display = init_display,
250 .set_var = set_var,
251 .set_gamma = set_gamma,
252 .blank = blank,
253 },
254};
255
256FBTFT_REGISTER_DRIVER(DRVNAME, "sitronix,st7789v", &display);
257
258MODULE_ALIAS("spi:" DRVNAME);
259MODULE_ALIAS("platform:" DRVNAME);
260MODULE_ALIAS("spi:st7789v");
261MODULE_ALIAS("platform:st7789v");
262
263MODULE_DESCRIPTION("FB driver for the ST7789V LCD Controller");
264MODULE_AUTHOR("Dennis Menschel");
265MODULE_LICENSE("GPL");
266