1
2
3
4
5
6
7
8
9
10
11#include <linux/module.h>
12#include <linux/kernel.h>
13#include <linux/init.h>
14#include <linux/delay.h>
15#include <video/mipi_display.h>
16
17#include "fbtft.h"
18
19#define DRVNAME "fb_ili9163"
20#define WIDTH 128
21#define HEIGHT 128
22#define BPP 16
23#define FPS 30
24
25#ifdef GAMMA_ADJ
26#define GAMMA_LEN 15
27#define GAMMA_NUM 1
28#define DEFAULT_GAMMA "36 29 12 22 1C 15 42 B7 2F 13 12 0A 11 0B 06\n"
29#endif
30
31
32#define CMD_FRMCTR1 0xB1
33
34#define CMD_FRMCTR2 0xB2
35#define CMD_FRMCTR3 0xB3
36
37#define CMD_DINVCTR 0xB4
38#define CMD_RGBBLK 0xB5
39#define CMD_DFUNCTR 0xB6
40#define CMD_SDRVDIR 0xB7
41#define CMD_GDRVDIR 0xB8
42
43#define CMD_PWCTR1 0xC0
44#define CMD_PWCTR2 0xC1
45#define CMD_PWCTR3 0xC2
46#define CMD_PWCTR4 0xC3
47#define CMD_PWCTR5 0xC4
48#define CMD_VCOMCTR1 0xC5
49#define CMD_VCOMCTR2 0xC6
50#define CMD_VCOMOFFS 0xC7
51#define CMD_PGAMMAC 0xE0
52#define CMD_NGAMMAC 0xE1
53#define CMD_GAMRSEL 0xF2
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69#ifdef RED
70#define __OFFSET 32
71#else
72#define __OFFSET 0
73#endif
74
75static int init_display(struct fbtft_par *par)
76{
77 par->fbtftops.reset(par);
78
79 write_reg(par, MIPI_DCS_SOFT_RESET);
80 mdelay(500);
81 write_reg(par, MIPI_DCS_EXIT_SLEEP_MODE);
82 mdelay(5);
83 write_reg(par, MIPI_DCS_SET_PIXEL_FORMAT, MIPI_DCS_PIXEL_FMT_16BIT);
84
85 write_reg(par, MIPI_DCS_SET_GAMMA_CURVE, 0x02);
86#ifdef GAMMA_ADJ
87 write_reg(par, CMD_GAMRSEL, 0x01);
88#endif
89 write_reg(par, MIPI_DCS_ENTER_NORMAL_MODE);
90 write_reg(par, CMD_DFUNCTR, 0xff, 0x06);
91
92 write_reg(par, CMD_FRMCTR1, 0x08, 0x02);
93 write_reg(par, CMD_DINVCTR, 0x07);
94
95 write_reg(par, CMD_PWCTR1, 0x0A, 0x02);
96
97 write_reg(par, CMD_PWCTR2, 0x02);
98
99 write_reg(par, CMD_VCOMCTR1, 0x50, 0x63);
100 write_reg(par, CMD_VCOMOFFS, 0);
101
102 write_reg(par, MIPI_DCS_SET_COLUMN_ADDRESS, 0, 0, 0, WIDTH);
103 write_reg(par, MIPI_DCS_SET_PAGE_ADDRESS, 0, 0, 0, HEIGHT);
104
105 write_reg(par, MIPI_DCS_SET_DISPLAY_ON);
106 write_reg(par, MIPI_DCS_WRITE_MEMORY_START);
107
108 return 0;
109}
110
111static void set_addr_win(struct fbtft_par *par, int xs, int ys,
112 int xe, int ye)
113{
114 switch (par->info->var.rotate) {
115 case 0:
116 write_reg(par, MIPI_DCS_SET_COLUMN_ADDRESS,
117 xs >> 8, xs & 0xff, xe >> 8, xe & 0xff);
118 write_reg(par, MIPI_DCS_SET_PAGE_ADDRESS,
119 (ys + __OFFSET) >> 8, (ys + __OFFSET) & 0xff,
120 (ye + __OFFSET) >> 8, (ye + __OFFSET) & 0xff);
121 break;
122 case 90:
123 write_reg(par, MIPI_DCS_SET_COLUMN_ADDRESS,
124 (xs + __OFFSET) >> 8, (xs + __OFFSET) & 0xff,
125 (xe + __OFFSET) >> 8, (xe + __OFFSET) & 0xff);
126 write_reg(par, MIPI_DCS_SET_PAGE_ADDRESS,
127 ys >> 8, ys & 0xff, ye >> 8, ye & 0xff);
128 break;
129 case 180:
130 case 270:
131 write_reg(par, MIPI_DCS_SET_COLUMN_ADDRESS,
132 xs >> 8, xs & 0xff, xe >> 8, xe & 0xff);
133 write_reg(par, MIPI_DCS_SET_PAGE_ADDRESS,
134 ys >> 8, ys & 0xff, ye >> 8, ye & 0xff);
135 break;
136 default:
137
138 par->info->var.rotate = 0;
139 }
140 write_reg(par, MIPI_DCS_WRITE_MEMORY_START);
141}
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163static int set_var(struct fbtft_par *par)
164{
165 u8 mactrl_data = 0;
166
167 switch (par->info->var.rotate) {
168 case 0:
169 mactrl_data = 0x08;
170 break;
171 case 180:
172 mactrl_data = 0xC8;
173 break;
174 case 270:
175 mactrl_data = 0xA8;
176 break;
177 case 90:
178 mactrl_data = 0x68;
179 break;
180 }
181
182
183 if (par->bgr)
184 mactrl_data |= BIT(2);
185 write_reg(par, MIPI_DCS_SET_ADDRESS_MODE, mactrl_data);
186 write_reg(par, MIPI_DCS_WRITE_MEMORY_START);
187 return 0;
188}
189
190#ifdef GAMMA_ADJ
191#define CURVE(num, idx) curves[(num) * par->gamma.num_values + (idx)]
192static int gamma_adj(struct fbtft_par *par, u32 *curves)
193{
194 static const unsigned long mask[] = {
195 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
196 0x1f, 0x3f, 0x0f, 0x0f, 0x7f, 0x1f,
197 0x3F, 0x3F, 0x3F, 0x3F, 0x3F};
198 int i, j;
199
200 for (i = 0; i < GAMMA_NUM; i++)
201 for (j = 0; j < GAMMA_LEN; j++)
202 CURVE(i, j) &= mask[i * par->gamma.num_values + j];
203
204 write_reg(par, CMD_PGAMMAC,
205 CURVE(0, 0),
206 CURVE(0, 1),
207 CURVE(0, 2),
208 CURVE(0, 3),
209 CURVE(0, 4),
210 CURVE(0, 5),
211 CURVE(0, 6),
212 (CURVE(0, 7) << 4) | CURVE(0, 8),
213 CURVE(0, 9),
214 CURVE(0, 10),
215 CURVE(0, 11),
216 CURVE(0, 12),
217 CURVE(0, 13),
218 CURVE(0, 14),
219 CURVE(0, 15));
220
221
222 write_reg(par, MIPI_DCS_WRITE_MEMORY_START);
223
224 return 0;
225}
226
227#undef CURVE
228#endif
229
230static struct fbtft_display display = {
231 .regwidth = 8,
232 .width = WIDTH,
233 .height = HEIGHT,
234 .bpp = BPP,
235 .fps = FPS,
236#ifdef GAMMA_ADJ
237 .gamma_num = GAMMA_NUM,
238 .gamma_len = GAMMA_LEN,
239 .gamma = DEFAULT_GAMMA,
240#endif
241 .fbtftops = {
242 .init_display = init_display,
243 .set_addr_win = set_addr_win,
244 .set_var = set_var,
245#ifdef GAMMA_ADJ
246 .set_gamma = gamma_adj,
247#endif
248 },
249};
250
251FBTFT_REGISTER_DRIVER(DRVNAME, "ilitek,ili9163", &display);
252
253MODULE_ALIAS("spi:" DRVNAME);
254MODULE_ALIAS("platform:" DRVNAME);
255MODULE_ALIAS("spi:ili9163");
256MODULE_ALIAS("platform:ili9163");
257
258MODULE_DESCRIPTION("FB driver for the ILI9163 LCD Controller");
259MODULE_AUTHOR("Kozhevnikov Anatoly");
260MODULE_LICENSE("GPL");
261