1 /*
2 * drivers/video/fbdev/sunxi/disp2/disp/lcd/st7789v/st7789v.c
3 *
4 * Copyright (c) 2007-2018 Allwinnertech Co., Ltd.
5 * Author: zhengxiaobin <zhengxiaobin@allwinnertech.com>
6 *
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 [lcd0]
17 lcd_used = 1
18
19 lcd_driver_name = "st7789v"
20 lcd_x = 240
21 lcd_y = 320
22 lcd_width = 108
23 lcd_height = 64
24 lcd_dclk_freq = 19
25
26 lcd_pwm_used = 1
27 lcd_pwm_ch = 8
28 lcd_pwm_freq = 50000
29 lcd_pwm_pol = 0
30 lcd_pwm_max_limit = 255
31
32 lcd_hbp = 120
33 ;10 + 20 + 10 + 240*3 = 760 real set 1000
34 lcd_ht = 850
35 lcd_hspw = 2
36 lcd_vbp = 13
37 lcd_vt = 373
38 lcd_vspw = 2
39
40 lcd_frm = 1
41 lcd_if = 0
42 lcd_hv_if = 8
43 lcd_hv_clk_phase = 0
44 lcd_hv_sync_polarity = 0
45 lcd_hv_srgb_seq = 0
46
47 lcd_io_phase = 0x0000
48 lcd_gamma_en = 0
49 lcd_bright_curve_en = 1
50 lcd_cmap_en = 0
51
52
53 lcd_rb_swap = 0
54
55 deu_mode = 0
56 lcdgamma4iep = 22
57 smart_color = 90
58
59 ;lcd_bl_en = port:PB03<1><0><default><1>
60 lcd_power = "vcc-lcd"
61 lcd_pin_power = "vcc-pd"
62
63 ;reset
64 lcd_gpio_0 = port:PD09<1><0><default><1>
65 ;cs
66 lcd_gpio_1 = port:PD10<1><0><default><0>
67 ;sda
68 lcd_gpio_2 = port:PD13<1><0><default><0>
69 ;sck
70 lcd_gpio_3 = port:PD12<1><0><default><0>
71
72
73 lcdd5 = port:PD03<2><0><2><default>
74 lcdd6 = port:PD04<2><0><2><default>
75 lcdd7 = port:PD05<2><0><2><default>
76 lcdd10 = port:PD06<2><0><2><default>
77 lcdd11 = port:PD07<2><0><2><default>
78 lcdd12 = port:PD08<2><0><2><default>
79
80 lcdclk = port:PD18<2><0><3><default>
81 lcdde = port:PD19<2><0><3><default>
82 lcdhsync = port:PD20<2><0><3><default>
83 lcdvsync = port:PD21<2><0><3><default>
84
85 [lcd0_suspend]
86
87 lcdd5 = port:PD03<7><0><2><default>
88 lcdd6 = port:PD04<7><0><2><default>
89 lcdd7 = port:PD05<7><0><2><default>
90 lcdd10 = port:PD06<7><0><2><default>
91 lcdd11 = port:PD07<7><0><2><default>
92 lcdd12 = port:PD08<7><0><2><default>
93
94 lcdclk = port:PD18<7><0><3><default>
95 lcdde = port:PD19<7><0><3><default>
96 lcdhsync = port:PD20<7><0><3><default>
97 lcdvsync = port:PD21<7><0><3><default>
98 */
99 #include "st7789v.h"
100
101 #define spi_scl_1 sunxi_lcd_gpio_set_value(0, 3, 1)
102 #define spi_scl_0 sunxi_lcd_gpio_set_value(0, 3, 0)
103 #define spi_sdi_1 sunxi_lcd_gpio_set_value(0, 2, 1)
104 #define spi_sdi_0 sunxi_lcd_gpio_set_value(0, 2, 0)
105 #define spi_cs_1 sunxi_lcd_gpio_set_value(0, 1, 1)
106 #define spi_cs_0 sunxi_lcd_gpio_set_value(0, 1, 0)
107
108 static void lcd_panel_st7789v_init(void);
109 static void LCD_power_on(u32 sel);
110 static void LCD_power_off(u32 sel);
111 static void LCD_bl_open(u32 sel);
112 static void LCD_bl_close(u32 sel);
113
114 static void LCD_panel_init(u32 sel);
115 static void LCD_panel_exit(u32 sel);
116
LCD_cfg_panel_info(struct panel_extend_para * info)117 static void LCD_cfg_panel_info(struct panel_extend_para *info)
118 {
119 u32 i = 0, j = 0;
120 u32 items;
121 u8 lcd_gamma_tbl[][2] = {
122 /* {input value, corrected value} */
123 {0, 0}, {15, 15}, {30, 30}, {45, 45}, {60, 60},
124 {75, 75}, {90, 90}, {105, 105}, {120, 120}, {135, 135},
125 {150, 150}, {165, 165}, {180, 180}, {195, 195}, {210, 210},
126 {225, 225}, {240, 240}, {255, 255},
127 };
128
129 u32 lcd_cmap_tbl[2][3][4] = {
130 {
131 {LCD_CMAP_G0, LCD_CMAP_B1, LCD_CMAP_G2, LCD_CMAP_B3},
132 {LCD_CMAP_B0, LCD_CMAP_R1, LCD_CMAP_B2, LCD_CMAP_R3},
133 {LCD_CMAP_R0, LCD_CMAP_G1, LCD_CMAP_R2, LCD_CMAP_G3},
134 },
135 {
136 {LCD_CMAP_B3, LCD_CMAP_G2, LCD_CMAP_B1, LCD_CMAP_G0},
137 {LCD_CMAP_R3, LCD_CMAP_B2, LCD_CMAP_R1, LCD_CMAP_B0},
138 {LCD_CMAP_G3, LCD_CMAP_R2, LCD_CMAP_G1, LCD_CMAP_R0},
139 },
140 };
141
142 items = sizeof(lcd_gamma_tbl) / 2;
143 for (i = 0; i < items - 1; i++) {
144 u32 num = lcd_gamma_tbl[i + 1][0] - lcd_gamma_tbl[i][0];
145
146 for (j = 0; j < num; j++) {
147 u32 value = 0;
148
149 value =
150 lcd_gamma_tbl[i][1] +
151 ((lcd_gamma_tbl[i + 1][1] - lcd_gamma_tbl[i][1]) *
152 j) /
153 num;
154 info->lcd_gamma_tbl[lcd_gamma_tbl[i][0] + j] =
155 (value << 16) + (value << 8) + value;
156 }
157 }
158 info->lcd_gamma_tbl[255] = (lcd_gamma_tbl[items - 1][1] << 16) +
159 (lcd_gamma_tbl[items - 1][1] << 8) +
160 lcd_gamma_tbl[items - 1][1];
161
162 memcpy(info->lcd_cmap_tbl, lcd_cmap_tbl, sizeof(lcd_cmap_tbl));
163 }
164
LCD_open_flow(u32 sel)165 static s32 LCD_open_flow(u32 sel)
166 {
167 /* open lcd power, and delay 50ms */
168 LCD_OPEN_FUNC(sel, LCD_power_on, 200);
169 /* open lcd power, than delay 200ms */
170 LCD_OPEN_FUNC(sel, LCD_panel_init, 200);
171 /* open lcd controller, and delay 100ms */
172 LCD_OPEN_FUNC(sel, sunxi_lcd_tcon_enable, 150);
173 /* open lcd backlight, and delay 0ms */
174 LCD_OPEN_FUNC(sel, LCD_bl_open, 0);
175
176 return 0;
177 }
178
LCD_close_flow(u32 sel)179 static s32 LCD_close_flow(u32 sel)
180 {
181 /* close lcd backlight, and delay 0ms */
182 LCD_CLOSE_FUNC(sel, LCD_bl_close, 50);
183 /* close lcd controller, and delay 0ms */
184 LCD_CLOSE_FUNC(sel, sunxi_lcd_tcon_disable, 10);
185 /* open lcd power, than delay 200ms */
186 LCD_CLOSE_FUNC(sel, LCD_panel_exit, 10);
187 /* close lcd power, and delay 500ms */
188 LCD_CLOSE_FUNC(sel, LCD_power_off, 10);
189
190 return 0;
191 }
192
LCD_power_on(u32 sel)193 static void LCD_power_on(u32 sel)
194 {
195 /* config lcd_power pin to open lcd power0 */
196 sunxi_lcd_power_enable(sel, 0);
197 /* pwr_en, active low */
198 sunxi_lcd_gpio_set_value(sel, 3, 0);
199 sunxi_lcd_pin_cfg(sel, 1);
200 }
201
LCD_power_off(u32 sel)202 static void LCD_power_off(u32 sel)
203 {
204 sunxi_lcd_pin_cfg(sel, 0);
205 /* pwr_en, active low */
206 sunxi_lcd_gpio_set_value(sel, 3, 1);
207 /* config lcd_power pin to close lcd power0 */
208 sunxi_lcd_power_disable(sel, 0);
209 }
210
LCD_bl_open(u32 sel)211 static void LCD_bl_open(u32 sel)
212 {
213 sunxi_lcd_pwm_enable(sel);
214 /* config lcd_bl_en pin to open lcd backlight */
215 sunxi_lcd_backlight_enable(sel);
216 }
217
LCD_bl_close(u32 sel)218 static void LCD_bl_close(u32 sel)
219 {
220 /* config lcd_bl_en pin to close lcd backlight */
221 sunxi_lcd_backlight_disable(sel);
222 sunxi_lcd_pwm_disable(sel);
223 }
224
st7789v_spi_write_cmd(u8 value)225 static void st7789v_spi_write_cmd(u8 value)
226 {
227 int i;
228
229 spi_cs_0;
230 spi_scl_0;
231
232 spi_sdi_0;
233 spi_scl_1;
234 spi_scl_0;
235
236 for (i = 0; i < 8; i++) {
237 if (value & 0x80)
238 spi_sdi_1;
239 else
240 spi_sdi_0;
241 value <<= 1;
242
243 spi_scl_1;
244 spi_scl_0;
245 }
246 spi_cs_1;
247 }
248
st7789v_spi_write_data(u8 value)249 static void st7789v_spi_write_data(u8 value)
250 {
251 int i;
252
253 spi_cs_0;
254 spi_scl_0;
255
256 spi_sdi_1;
257 spi_scl_1;
258 spi_scl_0;
259
260 for (i = 0; i < 8; i++) {
261 if (value & 0x80)
262 spi_sdi_1;
263 else
264 spi_sdi_0;
265 value <<= 1;
266
267 spi_scl_1;
268 spi_scl_0;
269 }
270 spi_cs_1;
271 }
272
LCD_panel_init(u32 sel)273 static void LCD_panel_init(u32 sel)
274 {
275 struct disp_panel_para *info =
276 kmalloc(sizeof(struct disp_panel_para), GFP_KERNEL | __GFP_ZERO);
277 bsp_disp_get_panel_info(sel, info);
278 lcd_panel_st7789v_init();
279 disp_sys_free(info);
280 return;
281 }
282
LCD_panel_exit(u32 sel)283 static void LCD_panel_exit(u32 sel)
284 {
285 struct disp_panel_para *info =
286 kmalloc(sizeof(struct disp_panel_para), GFP_KERNEL | __GFP_ZERO);
287 st7789v_spi_write_cmd(0x28);
288 st7789v_spi_write_cmd(0x10);
289 sunxi_lcd_delay_ms(300);
290 bsp_disp_get_panel_info(sel, info);
291 disp_sys_free(info);
292 return;
293 }
294
lcd_panel_st7789v_init(void)295 static void lcd_panel_st7789v_init(void)
296 {
297 /* VCI=3.3V */
298 /************* Reset LCD Driver ****************/
299 spi_cs_0;
300 sunxi_lcd_gpio_set_value(0, 0, 1);
301 sunxi_lcd_delay_ms(50);
302 sunxi_lcd_gpio_set_value(0, 0, 0);
303 /* Delay 10ms, This delay time is necessary */
304 sunxi_lcd_delay_ms(100);
305 sunxi_lcd_gpio_set_value(0, 0, 1);
306 /* Delay 120 ms */
307 sunxi_lcd_delay_ms(150);
308
309 st7789v_spi_write_cmd(0x01);
310 sunxi_lcd_delay_ms(50);
311 st7789v_spi_write_cmd(0x11);
312 sunxi_lcd_delay_ms(120);
313
314 st7789v_spi_write_cmd(0x36);
315 st7789v_spi_write_data(0xd4);
316 st7789v_spi_write_cmd(0x3a);
317 st7789v_spi_write_data(0x06);
318 st7789v_spi_write_cmd(0x20);
319
320 st7789v_spi_write_cmd(0xb0);
321 st7789v_spi_write_data(0x11);
322 st7789v_spi_write_data(0xe4);
323
324 st7789v_spi_write_cmd(0xb1);
325 st7789v_spi_write_data(0xc0);
326 st7789v_spi_write_data(0x02);
327 st7789v_spi_write_data(0x04);
328
329 st7789v_spi_write_cmd(0xb7);
330 st7789v_spi_write_data(0x35);
331 st7789v_spi_write_cmd(0xbb);
332 st7789v_spi_write_data(0x20);
333 st7789v_spi_write_cmd(0xC0);
334 st7789v_spi_write_data(0x21);
335
336 st7789v_spi_write_cmd(0xC2);
337 /* VDV and VRH Command Enable */
338 st7789v_spi_write_data(0x01);
339
340 st7789v_spi_write_cmd(0xC3);
341 st7789v_spi_write_data(0x0b);
342
343 st7789v_spi_write_cmd(0xC4);
344 st7789v_spi_write_data(0x20);
345
346 st7789v_spi_write_cmd(0xc6);
347 st7789v_spi_write_data(0x0f);
348
349 st7789v_spi_write_cmd(0xd0);
350 st7789v_spi_write_data(0xa4);
351 st7789v_spi_write_data(0xa1);
352
353 st7789v_spi_write_cmd(0xE0);
354 st7789v_spi_write_data(0x0D);
355 st7789v_spi_write_data(0x10);
356 st7789v_spi_write_data(0x13);
357 st7789v_spi_write_data(0x08);
358 st7789v_spi_write_data(0x07);
359 st7789v_spi_write_data(0x04);
360 st7789v_spi_write_data(0x2C);
361 st7789v_spi_write_data(0x33);
362 st7789v_spi_write_data(0x42);
363 st7789v_spi_write_data(0x29);
364 st7789v_spi_write_data(0x17);
365 st7789v_spi_write_data(0x18);
366 st7789v_spi_write_data(0x24);
367 st7789v_spi_write_data(0x28);
368
369 st7789v_spi_write_cmd(0xE1);
370 st7789v_spi_write_data(0x0D);
371 st7789v_spi_write_data(0x10);
372 st7789v_spi_write_data(0x12);
373 st7789v_spi_write_data(0x08);
374 st7789v_spi_write_data(0x07);
375 st7789v_spi_write_data(0x03);
376 st7789v_spi_write_data(0x2B);
377 st7789v_spi_write_data(0x32);
378 st7789v_spi_write_data(0x40);
379 st7789v_spi_write_data(0x28);
380 st7789v_spi_write_data(0x17);
381 st7789v_spi_write_data(0x16);
382 st7789v_spi_write_data(0x26);
383 st7789v_spi_write_data(0x28);
384
385 st7789v_spi_write_cmd(0x2a);
386 st7789v_spi_write_data(0x00);
387 st7789v_spi_write_data(0x00);
388 st7789v_spi_write_data(0x00);
389 st7789v_spi_write_data(0xef);
390
391 st7789v_spi_write_cmd(0x2b);
392 st7789v_spi_write_data(0x00);
393 st7789v_spi_write_data(0x00);
394 st7789v_spi_write_data(0x01);
395 st7789v_spi_write_data(0x3f);
396
397 /* Exit Sleep */
398 st7789v_spi_write_cmd(0x21);
399 sunxi_lcd_delay_ms(100);
400 st7789v_spi_write_cmd(0x11);
401 sunxi_lcd_delay_ms(50);
402 st7789v_spi_write_cmd(0x29);
403 sunxi_lcd_delay_ms(50);
404 }
405
406 /* sel: 0:lcd0; 1:lcd1 */
LCD_user_defined_func(u32 sel,u32 para1,u32 para2,u32 para3)407 static s32 LCD_user_defined_func(u32 sel, u32 para1, u32 para2, u32 para3)
408 {
409 return 0;
410 }
411
412 struct __lcd_panel st7789v_panel = {
413 /* panel driver name, must mach the name of lcd_drv_name in sys_config.fex
414 */
415 .name = "st7789v",
416 .func = {
417 .cfg_panel_info = LCD_cfg_panel_info,
418 .cfg_open_flow = LCD_open_flow,
419 .cfg_close_flow = LCD_close_flow,
420 .lcd_user_defined_func = LCD_user_defined_func,
421 },
422 };
423