xref: /openbmc/u-boot/drivers/video/lg4573.c (revision 699c4e59)
1 /*
2  * LCD: LG4573, TFT 4.3", 480x800, RGB24
3  * LCD initialization via SPI
4  *
5  * SPDX-License-Identifier:	GPL-2.0
6  *
7  */
8 #include <common.h>
9 #include <errno.h>
10 #include <spi.h>
11 
12 #define PWR_ON_DELAY_MSECS  120
13 
14 static int lb043wv_spi_write_u16(struct spi_slave *spi, u16 val)
15 {
16 	unsigned long flags = SPI_XFER_BEGIN;
17 	unsigned short buf16 = htons(val);
18 	int ret = 0;
19 
20 	flags |= SPI_XFER_END;
21 
22 	ret = spi_xfer(spi, 16, &buf16, NULL, flags);
23 	if (ret)
24 		debug("%s: Failed to send: %d\n", __func__, ret);
25 
26 	return ret;
27 }
28 
29 static void lb043wv_spi_write_u16_array(struct spi_slave *spi, u16 *buff,
30 					int size)
31 {
32 	int i;
33 
34 	for (i = 0; i < size; i++)
35 		lb043wv_spi_write_u16(spi, buff[i]);
36 }
37 
38 static void lb043wv_display_mode_settings(struct spi_slave *spi)
39 {
40 	static u16 display_mode_settings[] = {
41 	  0x703A,
42 	  0x7270,
43 	  0x70B1,
44 	  0x7208,
45 	  0x723B,
46 	  0x720F,
47 	  0x70B2,
48 	  0x7200,
49 	  0x72C8,
50 	  0x70B3,
51 	  0x7200,
52 	  0x70B4,
53 	  0x7200,
54 	  0x70B5,
55 	  0x7242,
56 	  0x7210,
57 	  0x7210,
58 	  0x7200,
59 	  0x7220,
60 	  0x70B6,
61 	  0x720B,
62 	  0x720F,
63 	  0x723C,
64 	  0x7213,
65 	  0x7213,
66 	  0x72E8,
67 	  0x70B7,
68 	  0x7246,
69 	  0x7206,
70 	  0x720C,
71 	  0x7200,
72 	  0x7200,
73 	};
74 
75 	debug("transfer display mode settings\n");
76 	lb043wv_spi_write_u16_array(spi, display_mode_settings,
77 				    ARRAY_SIZE(display_mode_settings));
78 }
79 
80 static void lb043wv_power_settings(struct spi_slave *spi)
81 {
82 	static u16 power_settings[] = {
83 	  0x70C0,
84 	  0x7201,
85 	  0x7211,
86 	  0x70C3,
87 	  0x7207,
88 	  0x7203,
89 	  0x7204,
90 	  0x7204,
91 	  0x7204,
92 	  0x70C4,
93 	  0x7212,
94 	  0x7224,
95 	  0x7218,
96 	  0x7218,
97 	  0x7202,
98 	  0x7249,
99 	  0x70C5,
100 	  0x726F,
101 	  0x70C6,
102 	  0x7241,
103 	  0x7263,
104 	};
105 
106 	debug("transfer power settings\n");
107 	lb043wv_spi_write_u16_array(spi, power_settings,
108 				    ARRAY_SIZE(power_settings));
109 }
110 
111 static void lb043wv_gamma_settings(struct spi_slave *spi)
112 {
113 	static u16 gamma_settings[] = {
114 	  0x70D0,
115 	  0x7203,
116 	  0x7207,
117 	  0x7273,
118 	  0x7235,
119 	  0x7200,
120 	  0x7201,
121 	  0x7220,
122 	  0x7200,
123 	  0x7203,
124 	  0x70D1,
125 	  0x7203,
126 	  0x7207,
127 	  0x7273,
128 	  0x7235,
129 	  0x7200,
130 	  0x7201,
131 	  0x7220,
132 	  0x7200,
133 	  0x7203,
134 	  0x70D2,
135 	  0x7203,
136 	  0x7207,
137 	  0x7273,
138 	  0x7235,
139 	  0x7200,
140 	  0x7201,
141 	  0x7220,
142 	  0x7200,
143 	  0x7203,
144 	  0x70D3,
145 	  0x7203,
146 	  0x7207,
147 	  0x7273,
148 	  0x7235,
149 	  0x7200,
150 	  0x7201,
151 	  0x7220,
152 	  0x7200,
153 	  0x7203,
154 	  0x70D4,
155 	  0x7203,
156 	  0x7207,
157 	  0x7273,
158 	  0x7235,
159 	  0x7200,
160 	  0x7201,
161 	  0x7220,
162 	  0x7200,
163 	  0x7203,
164 	  0x70D5,
165 	  0x7203,
166 	  0x7207,
167 	  0x7273,
168 	  0x7235,
169 	  0x7200,
170 	  0x7201,
171 	  0x7220,
172 	  0x7200,
173 	  0x7203,
174 	};
175 
176 	debug("transfer gamma settings\n");
177 	lb043wv_spi_write_u16_array(spi, gamma_settings,
178 				    ARRAY_SIZE(gamma_settings));
179 }
180 
181 static void lb043wv_display_on(struct spi_slave *spi)
182 {
183 	static u16 sleep_out = 0x7011;
184 	static u16 display_on = 0x7029;
185 
186 	lb043wv_spi_write_u16(spi, sleep_out);
187 	mdelay(PWR_ON_DELAY_MSECS);
188 	lb043wv_spi_write_u16(spi, display_on);
189 }
190 
191 int lg4573_spi_startup(unsigned int bus, unsigned int cs,
192 	unsigned int max_hz, unsigned int spi_mode)
193 {
194 	struct spi_slave *spi;
195 	int ret;
196 
197 	spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
198 	if (!spi) {
199 		debug("%s: Failed to set up slave\n", __func__);
200 		return -1;
201 	}
202 
203 	ret = spi_claim_bus(spi);
204 	if (ret) {
205 		debug("%s: Failed to claim SPI bus: %d\n", __func__, ret);
206 		goto err_claim_bus;
207 	}
208 
209 	lb043wv_display_mode_settings(spi);
210 	lb043wv_power_settings(spi);
211 	lb043wv_gamma_settings(spi);
212 
213 	lb043wv_display_on(spi);
214 	return 0;
215 err_claim_bus:
216 	spi_free_slave(spi);
217 	return -1;
218 }
219 
220 static int do_lgset(cmd_tbl_t *cmdtp, int flag, int argc,
221 		       char * const argv[])
222 {
223 	lg4573_spi_startup(CONFIG_LG4573_BUS, CONFIG_LG4573_CS, 10000000,
224 			   SPI_MODE_0);
225 	return 0;
226 }
227 
228 U_BOOT_CMD(
229 	lgset,	2,	1,	do_lgset,
230 	"set lgdisplay",
231 	""
232 );
233