1 /*
2  * Allwinner SoCs display driver.
3  *
4  * Copyright (C) 2016 Allwinner.
5  *
6  * This file is licensed under the terms of the GNU General Public
7  * License version 2.  This program is licensed "as is" without any
8  * warranty of any kind, whether express or implied.
9  */
10 
11 #include "t27p06.h"
12 
13 #define t27p06_spi_scl_1    sunxi_lcd_gpio_set_value(0, 2, 1)
14 #define t27p06_spi_scl_0    sunxi_lcd_gpio_set_value(0, 2, 0)
15 #define t27p06_spi_sdi_1    sunxi_lcd_gpio_set_value(0, 1, 1)
16 #define t27p06_spi_sdi_0    sunxi_lcd_gpio_set_value(0, 1, 0)
17 #define t27p06_spi_cs_1 sunxi_lcd_gpio_set_value(0, 0, 1)
18 #define t27p06_spi_cs_0 sunxi_lcd_gpio_set_value(0, 0, 0)
19 
20 static void t27p06_spi_wr(u32 value);
21 static void lcd_panel_t27p06_init(void);
22 static void LCD_power_on(u32 sel);
23 static void LCD_power_off(u32 sel);
24 static void LCD_bl_open(u32 sel);
25 static void LCD_bl_close(u32 sel);
26 
27 static void LCD_panel_init(u32 sel);
28 static void LCD_panel_exit(u32 sel);
29 
LCD_cfg_panel_info(struct panel_extend_para * info)30 static void LCD_cfg_panel_info(struct panel_extend_para *info)
31 {
32     u32 i = 0, j = 0;
33     u32 items;
34     u8 lcd_gamma_tbl[][2] = {
35         /* {input value, corrected value} */
36         {0, 0},
37         {15, 15},
38         {30, 30},
39         {45, 45},
40         {60, 60},
41         {75, 75},
42         {90, 90},
43         {105, 105},
44         {120, 120},
45         {135, 135},
46         {150, 150},
47         {165, 165},
48         {180, 180},
49         {195, 195},
50         {210, 210},
51         {225, 225},
52         {240, 240},
53         {255, 255},
54     };
55 
56     u32 lcd_cmap_tbl[2][3][4] = {
57         {
58          {LCD_CMAP_G0, LCD_CMAP_B1, LCD_CMAP_G2, LCD_CMAP_B3},
59          {LCD_CMAP_B0, LCD_CMAP_R1, LCD_CMAP_B2, LCD_CMAP_R3},
60          {LCD_CMAP_R0, LCD_CMAP_G1, LCD_CMAP_R2, LCD_CMAP_G3},
61          },
62         {
63          {LCD_CMAP_B3, LCD_CMAP_G2, LCD_CMAP_B1, LCD_CMAP_G0},
64          {LCD_CMAP_R3, LCD_CMAP_B2, LCD_CMAP_R1, LCD_CMAP_B0},
65          {LCD_CMAP_G3, LCD_CMAP_R2, LCD_CMAP_G1, LCD_CMAP_R0},
66          },
67     };
68 
69     items = sizeof(lcd_gamma_tbl) / 2;
70     for (i = 0; i < items - 1; i++) {
71         u32 num = lcd_gamma_tbl[i + 1][0] - lcd_gamma_tbl[i][0];
72 
73         for (j = 0; j < num; j++) {
74             u32 value = 0;
75 
76             value =
77                 lcd_gamma_tbl[i][1] +
78                 ((lcd_gamma_tbl[i + 1][1] -
79                   lcd_gamma_tbl[i][1]) * j) / num;
80             info->lcd_gamma_tbl[lcd_gamma_tbl[i][0] + j] =
81                 (value << 16) + (value << 8) + value;
82         }
83     }
84     info->lcd_gamma_tbl[255] =
85         (lcd_gamma_tbl[items - 1][1] << 16) +
86         (lcd_gamma_tbl[items - 1][1] << 8) + lcd_gamma_tbl[items - 1][1];
87 
88     memcpy(info->lcd_cmap_tbl, lcd_cmap_tbl, sizeof(lcd_cmap_tbl));
89 
90 }
91 
LCD_open_flow(u32 sel)92 static s32 LCD_open_flow(u32 sel)
93 {
94     /* open lcd power, and delay 50ms */
95     LCD_OPEN_FUNC(sel, LCD_power_on, 0);
96     /* open lcd power, than delay 200ms */
97     LCD_OPEN_FUNC(sel, LCD_panel_init, 10);
98     /* open lcd controller, and delay 100ms */
99     LCD_OPEN_FUNC(sel, sunxi_lcd_tcon_enable, 300);
100     /* open lcd backlight, and delay 0ms */
101     LCD_OPEN_FUNC(sel, LCD_bl_open, 0);
102 
103     return 0;
104 }
105 
LCD_close_flow(u32 sel)106 static s32 LCD_close_flow(u32 sel)
107 {
108     /* close lcd backlight, and delay 0ms */
109     LCD_CLOSE_FUNC(sel, LCD_bl_close, 300);
110     /* close lcd controller, and delay 0ms */
111     LCD_CLOSE_FUNC(sel, sunxi_lcd_tcon_disable, 10);
112     /* open lcd power, than delay 200ms */
113     LCD_CLOSE_FUNC(sel, LCD_panel_exit, 10);
114     /* close lcd power, and delay 500ms */
115     LCD_CLOSE_FUNC(sel, LCD_power_off, 500);
116 
117     return 0;
118 }
119 
LCD_power_on(u32 sel)120 static void LCD_power_on(u32 sel)
121 {
122     /* config lcd_power pin to open lcd power0 */
123     sunxi_lcd_power_enable(sel, 0);
124     /* pwr_en, active low */
125     sunxi_lcd_gpio_set_value(sel, 3, 0);
126     sunxi_lcd_pin_cfg(sel, 1);
127 }
128 
LCD_power_off(u32 sel)129 static void LCD_power_off(u32 sel)
130 {
131     sunxi_lcd_pin_cfg(sel, 0);
132     /* pwr_en, active low */
133     sunxi_lcd_gpio_set_value(sel, 3, 1);
134     /* config lcd_power pin to close lcd power0 */
135     sunxi_lcd_power_disable(sel, 0);
136 }
137 
LCD_bl_open(u32 sel)138 static void LCD_bl_open(u32 sel)
139 {
140     sunxi_lcd_pwm_enable(sel);
141     /* config lcd_bl_en pin to open lcd backlight */
142     sunxi_lcd_backlight_enable(sel);
143 }
144 
LCD_bl_close(u32 sel)145 static void LCD_bl_close(u32 sel)
146 {
147     /* config lcd_bl_en pin to close lcd backlight */
148     sunxi_lcd_backlight_disable(sel);
149     sunxi_lcd_pwm_disable(sel);
150 }
151 
LCD_panel_init(u32 sel)152 static void LCD_panel_init(u32 sel)
153 {
154     struct disp_panel_para *info =
155         kmalloc(sizeof(struct disp_panel_para), GFP_KERNEL | __GFP_ZERO);
156 
157     bsp_disp_get_panel_info(sel, info);
158     lcd_panel_t27p06_init();
159     disp_sys_free(info);
160 }
161 
LCD_panel_exit(u32 sel)162 static void LCD_panel_exit(u32 sel)
163 {
164     struct disp_panel_para *info =
165         kmalloc(sizeof(struct disp_panel_para), GFP_KERNEL | __GFP_ZERO);
166 
167     bsp_disp_get_panel_info(sel, info);
168     disp_sys_free(info);
169 }
170 
t27p06_spi_wr(u32 value)171 static void t27p06_spi_wr(u32 value)
172 {
173     u32 i;
174 
175     t27p06_spi_cs_1;
176     t27p06_spi_sdi_1;
177     t27p06_spi_scl_1;
178     sunxi_lcd_delay_us(10);
179     t27p06_spi_cs_0;
180     sunxi_lcd_delay_us(10);
181     for (i = 0; i < 16; i++) {
182         sunxi_lcd_delay_us(10);
183         t27p06_spi_scl_0;
184         if (value & 0x8000)
185             t27p06_spi_sdi_1;
186         else
187             t27p06_spi_sdi_0;
188         value <<= 1;
189         sunxi_lcd_delay_us(10);
190         t27p06_spi_scl_1;
191     }
192     sunxi_lcd_delay_us(10);
193     t27p06_spi_sdi_1;
194     t27p06_spi_cs_1;
195 }
lcd_panel_t27p06_init(void)196 static void lcd_panel_t27p06_init(void)
197 {
198     t27p06_spi_wr(0x055f);
199     sunxi_lcd_delay_ms(5);
200     t27p06_spi_wr(0x051f);  /* reset */
201     sunxi_lcd_delay_ms(10);
202     t27p06_spi_wr(0x055f);
203     sunxi_lcd_delay_ms(50);
204     t27p06_spi_wr(0x2b01);  /* exit standby mode */
205     t27p06_spi_wr(0x0009);  /* vcomac */
206     t27p06_spi_wr(0x019f);  /* vcomdc */
207 /* t27p06_spi_wr(0x040b);//8-bit rgb interface */
208     t27p06_spi_wr(0x040c);  /* 8-bit rgb interface */
209     t27p06_spi_wr(0x1604);  /* default gamma setting  2.2 */
210 }
211 
212 /* sel: 0:lcd0; 1:lcd1 */
LCD_user_defined_func(u32 sel,u32 para1,u32 para2,u32 para3)213 static s32 LCD_user_defined_func(u32 sel, u32 para1, u32 para2, u32 para3)
214 {
215     return 0;
216 }
217 
218 struct __lcd_panel t27p06_panel = {
219     /* panel driver name, must mach the lcd_drv_name in sys_config.fex */
220     .name = "t27p06",
221     .func = {
222          .cfg_panel_info = LCD_cfg_panel_info,
223          .cfg_open_flow = LCD_open_flow,
224          .cfg_close_flow = LCD_close_flow,
225          .lcd_user_defined_func = LCD_user_defined_func,
226          }
227     ,
228 };
229