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