1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2f1a74918SNikita Kiryanov /*
3f1a74918SNikita Kiryanov * scf0403.c -- support for DataImage SCF0403 LCD
4f1a74918SNikita Kiryanov *
5f1a74918SNikita Kiryanov * Copyright (c) 2013 Adapted from Linux driver:
6f1a74918SNikita Kiryanov * Copyright (c) 2012 Anders Electronics plc. All Rights Reserved.
7f1a74918SNikita Kiryanov * Copyright (c) 2012 CompuLab, Ltd
8f1a74918SNikita Kiryanov * Dmitry Lifshitz <lifshitz@compulab.co.il>
9f1a74918SNikita Kiryanov * Ilya Ledvich <ilya@compulab.co.il>
10f1a74918SNikita Kiryanov * Inspired by Alberto Panizzo <maramaopercheseimorto@gmail.com> &
11f1a74918SNikita Kiryanov * Marek Vasut work in l4f00242t03.c
12f1a74918SNikita Kiryanov *
13f1a74918SNikita Kiryanov * U-Boot port: Nikita Kiryanov <nikita@compulab.co.il>
14f1a74918SNikita Kiryanov */
15f1a74918SNikita Kiryanov
16f1a74918SNikita Kiryanov #include <common.h>
17f1a74918SNikita Kiryanov #include <asm/gpio.h>
18f1a74918SNikita Kiryanov #include <spi.h>
19f1a74918SNikita Kiryanov
20f1a74918SNikita Kiryanov struct scf0403_cmd {
21f1a74918SNikita Kiryanov u16 cmd;
22f1a74918SNikita Kiryanov u16 *params;
23f1a74918SNikita Kiryanov int count;
24f1a74918SNikita Kiryanov };
25f1a74918SNikita Kiryanov
26f1a74918SNikita Kiryanov struct scf0403_initseq_entry {
27f1a74918SNikita Kiryanov struct scf0403_cmd cmd;
28f1a74918SNikita Kiryanov int delay_ms;
29f1a74918SNikita Kiryanov };
30f1a74918SNikita Kiryanov
31f1a74918SNikita Kiryanov struct scf0403_priv {
32f1a74918SNikita Kiryanov struct spi_slave *spi;
33f1a74918SNikita Kiryanov unsigned int reset_gpio;
34f1a74918SNikita Kiryanov u32 rddid;
35f1a74918SNikita Kiryanov struct scf0403_initseq_entry *init_seq;
36f1a74918SNikita Kiryanov int seq_size;
37f1a74918SNikita Kiryanov };
38f1a74918SNikita Kiryanov
39f1a74918SNikita Kiryanov struct scf0403_priv priv;
40f1a74918SNikita Kiryanov
41f1a74918SNikita Kiryanov #define SCF0403852GGU04_ID 0x000080
42f1a74918SNikita Kiryanov
43f1a74918SNikita Kiryanov /* SCF0403526GGU20 model commands parameters */
44f1a74918SNikita Kiryanov static u16 extcmd_params_sn20[] = {0xff, 0x98, 0x06};
45f1a74918SNikita Kiryanov static u16 spiinttype_params_sn20[] = {0x60};
46f1a74918SNikita Kiryanov static u16 bc_params_sn20[] = {
47f1a74918SNikita Kiryanov 0x01, 0x10, 0x61, 0x74, 0x01, 0x01, 0x1B,
48f1a74918SNikita Kiryanov 0x12, 0x71, 0x00, 0x00, 0x00, 0x01, 0x01,
49f1a74918SNikita Kiryanov 0x05, 0x00, 0xFF, 0xF2, 0x01, 0x00, 0x40,
50f1a74918SNikita Kiryanov };
51f1a74918SNikita Kiryanov static u16 bd_params_sn20[] = {0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67};
52f1a74918SNikita Kiryanov static u16 be_params_sn20[] = {
53f1a74918SNikita Kiryanov 0x01, 0x22, 0x22, 0xBA, 0xDC, 0x26, 0x28, 0x22, 0x22,
54f1a74918SNikita Kiryanov };
55f1a74918SNikita Kiryanov static u16 vcom_params_sn20[] = {0x74};
56f1a74918SNikita Kiryanov static u16 vmesur_params_sn20[] = {0x7F, 0x0F, 0x00};
57f1a74918SNikita Kiryanov static u16 powerctl_params_sn20[] = {0x03, 0x0b, 0x00};
58f1a74918SNikita Kiryanov static u16 lvglvolt_params_sn20[] = {0x08};
59f1a74918SNikita Kiryanov static u16 engsetting_params_sn20[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x20};
60f1a74918SNikita Kiryanov static u16 dispfunc_params_sn20[] = {0xa0};
61f1a74918SNikita Kiryanov static u16 dvddvolt_params_sn20[] = {0x74};
62f1a74918SNikita Kiryanov static u16 dispinv_params_sn20[] = {0x00, 0x00, 0x00};
63f1a74918SNikita Kiryanov static u16 panelres_params_sn20[] = {0x82};
64f1a74918SNikita Kiryanov static u16 framerate_params_sn20[] = {0x00, 0x13, 0x13};
65f1a74918SNikita Kiryanov static u16 timing_params_sn20[] = {0x80, 0x05, 0x40, 0x28};
66f1a74918SNikita Kiryanov static u16 powerctl2_params_sn20[] = {0x17, 0x75, 0x79, 0x20};
67f1a74918SNikita Kiryanov static u16 memaccess_params_sn20[] = {0x00};
68f1a74918SNikita Kiryanov static u16 pixfmt_params_sn20[] = {0x66};
69f1a74918SNikita Kiryanov static u16 pgamma_params_sn20[] = {
70f1a74918SNikita Kiryanov 0x00, 0x03, 0x0b, 0x0c, 0x0e, 0x08, 0xc5, 0x04,
71f1a74918SNikita Kiryanov 0x08, 0x0c, 0x13, 0x11, 0x11, 0x14, 0x0c, 0x10,
72f1a74918SNikita Kiryanov };
73f1a74918SNikita Kiryanov static u16 ngamma_params_sn20[] = {
74f1a74918SNikita Kiryanov 0x00, 0x0d, 0x11, 0x0c, 0x0c, 0x04, 0x76, 0x03,
75f1a74918SNikita Kiryanov 0x08, 0x0b, 0x16, 0x10, 0x0d, 0x16, 0x0a, 0x00,
76f1a74918SNikita Kiryanov };
77f1a74918SNikita Kiryanov static u16 tearing_params_sn20[] = {0x00};
78f1a74918SNikita Kiryanov
79f1a74918SNikita Kiryanov /* SCF0403852GGU04 model commands parameters */
80f1a74918SNikita Kiryanov static u16 memaccess_params_sn04[] = {0x08};
81f1a74918SNikita Kiryanov static u16 pixfmt_params_sn04[] = {0x66};
82f1a74918SNikita Kiryanov static u16 modectl_params_sn04[] = {0x01};
83f1a74918SNikita Kiryanov static u16 dispfunc_params_sn04[] = {0x22, 0xe2, 0xFF, 0x04};
84f1a74918SNikita Kiryanov static u16 vcom_params_sn04[] = {0x00, 0x6A};
85f1a74918SNikita Kiryanov static u16 pgamma_params_sn04[] = {
86f1a74918SNikita Kiryanov 0x00, 0x07, 0x0d, 0x10, 0x13, 0x19, 0x0f, 0x0c,
87f1a74918SNikita Kiryanov 0x05, 0x08, 0x06, 0x13, 0x0f, 0x30, 0x20, 0x1f,
88f1a74918SNikita Kiryanov };
89f1a74918SNikita Kiryanov static u16 ngamma_params_sn04[] = {
90f1a74918SNikita Kiryanov 0x1F, 0x20, 0x30, 0x0F, 0x13, 0x06, 0x08, 0x05,
91f1a74918SNikita Kiryanov 0x0C, 0x0F, 0x19, 0x13, 0x10, 0x0D, 0x07, 0x00,
92f1a74918SNikita Kiryanov };
93f1a74918SNikita Kiryanov static u16 dispinv_params_sn04[] = {0x02};
94f1a74918SNikita Kiryanov
95f1a74918SNikita Kiryanov /* Common commands */
96f1a74918SNikita Kiryanov static struct scf0403_cmd scf0403_cmd_slpout = {0x11, NULL, 0};
97f1a74918SNikita Kiryanov static struct scf0403_cmd scf0403_cmd_dison = {0x29, NULL, 0};
98f1a74918SNikita Kiryanov
99f1a74918SNikita Kiryanov /* SCF0403852GGU04 init sequence */
100f1a74918SNikita Kiryanov static struct scf0403_initseq_entry scf0403_initseq_sn04[] = {
101f1a74918SNikita Kiryanov {{0x36, memaccess_params_sn04, ARRAY_SIZE(memaccess_params_sn04)}, 0},
102f1a74918SNikita Kiryanov {{0x3A, pixfmt_params_sn04, ARRAY_SIZE(pixfmt_params_sn04)}, 0},
103f1a74918SNikita Kiryanov {{0xB6, dispfunc_params_sn04, ARRAY_SIZE(dispfunc_params_sn04)}, 0},
104f1a74918SNikita Kiryanov {{0xC5, vcom_params_sn04, ARRAY_SIZE(vcom_params_sn04)}, 0},
105f1a74918SNikita Kiryanov {{0xE0, pgamma_params_sn04, ARRAY_SIZE(pgamma_params_sn04)}, 0},
106f1a74918SNikita Kiryanov {{0xE1, ngamma_params_sn04, ARRAY_SIZE(ngamma_params_sn04)}, 20},
107f1a74918SNikita Kiryanov {{0xB0, modectl_params_sn04, ARRAY_SIZE(modectl_params_sn04)}, 0},
108f1a74918SNikita Kiryanov {{0xB4, dispinv_params_sn04, ARRAY_SIZE(dispinv_params_sn04)}, 100},
109f1a74918SNikita Kiryanov };
110f1a74918SNikita Kiryanov
111f1a74918SNikita Kiryanov /* SCF0403526GGU20 init sequence */
112f1a74918SNikita Kiryanov static struct scf0403_initseq_entry scf0403_initseq_sn20[] = {
113f1a74918SNikita Kiryanov {{0xff, extcmd_params_sn20, ARRAY_SIZE(extcmd_params_sn20)}, 0},
114f1a74918SNikita Kiryanov {{0xba, spiinttype_params_sn20, ARRAY_SIZE(spiinttype_params_sn20)}, 0},
115f1a74918SNikita Kiryanov {{0xbc, bc_params_sn20, ARRAY_SIZE(bc_params_sn20)}, 0},
116f1a74918SNikita Kiryanov {{0xbd, bd_params_sn20, ARRAY_SIZE(bd_params_sn20)}, 0},
117f1a74918SNikita Kiryanov {{0xbe, be_params_sn20, ARRAY_SIZE(be_params_sn20)}, 0},
118f1a74918SNikita Kiryanov {{0xc7, vcom_params_sn20, ARRAY_SIZE(vcom_params_sn20)}, 0},
119f1a74918SNikita Kiryanov {{0xed, vmesur_params_sn20, ARRAY_SIZE(vmesur_params_sn20)}, 0},
120f1a74918SNikita Kiryanov {{0xc0, powerctl_params_sn20, ARRAY_SIZE(powerctl_params_sn20)}, 0},
121f1a74918SNikita Kiryanov {{0xfc, lvglvolt_params_sn20, ARRAY_SIZE(lvglvolt_params_sn20)}, 0},
122f1a74918SNikita Kiryanov {{0xb6, dispfunc_params_sn20, ARRAY_SIZE(dispfunc_params_sn20)}, 0},
123f1a74918SNikita Kiryanov {{0xdf, engsetting_params_sn20, ARRAY_SIZE(engsetting_params_sn20)}, 0},
124f1a74918SNikita Kiryanov {{0xf3, dvddvolt_params_sn20, ARRAY_SIZE(dvddvolt_params_sn20)}, 0},
125f1a74918SNikita Kiryanov {{0xb4, dispinv_params_sn20, ARRAY_SIZE(dispinv_params_sn20)}, 0},
126f1a74918SNikita Kiryanov {{0xf7, panelres_params_sn20, ARRAY_SIZE(panelres_params_sn20)}, 0},
127f1a74918SNikita Kiryanov {{0xb1, framerate_params_sn20, ARRAY_SIZE(framerate_params_sn20)}, 0},
128f1a74918SNikita Kiryanov {{0xf2, timing_params_sn20, ARRAY_SIZE(timing_params_sn20)}, 0},
129f1a74918SNikita Kiryanov {{0xc1, powerctl2_params_sn20, ARRAY_SIZE(powerctl2_params_sn20)}, 0},
130f1a74918SNikita Kiryanov {{0x36, memaccess_params_sn20, ARRAY_SIZE(memaccess_params_sn20)}, 0},
131f1a74918SNikita Kiryanov {{0x3a, pixfmt_params_sn20, ARRAY_SIZE(pixfmt_params_sn20)}, 0},
132f1a74918SNikita Kiryanov {{0xe0, pgamma_params_sn20, ARRAY_SIZE(pgamma_params_sn20)}, 0},
133f1a74918SNikita Kiryanov {{0xe1, ngamma_params_sn20, ARRAY_SIZE(ngamma_params_sn20)}, 0},
134f1a74918SNikita Kiryanov {{0x35, tearing_params_sn20, ARRAY_SIZE(tearing_params_sn20)}, 0},
135f1a74918SNikita Kiryanov };
136f1a74918SNikita Kiryanov
scf0403_gpio_reset(unsigned int gpio)137f1a74918SNikita Kiryanov static void scf0403_gpio_reset(unsigned int gpio)
138f1a74918SNikita Kiryanov {
139f1a74918SNikita Kiryanov if (!gpio_is_valid(gpio))
140f1a74918SNikita Kiryanov return;
141f1a74918SNikita Kiryanov
142f1a74918SNikita Kiryanov gpio_set_value(gpio, 1);
143f1a74918SNikita Kiryanov mdelay(100);
144f1a74918SNikita Kiryanov gpio_set_value(gpio, 0);
145f1a74918SNikita Kiryanov mdelay(40);
146f1a74918SNikita Kiryanov gpio_set_value(gpio, 1);
147f1a74918SNikita Kiryanov mdelay(100);
148f1a74918SNikita Kiryanov }
149f1a74918SNikita Kiryanov
scf0403_spi_read_rddid(struct spi_slave * spi,u32 * rddid)150f1a74918SNikita Kiryanov static int scf0403_spi_read_rddid(struct spi_slave *spi, u32 *rddid)
151f1a74918SNikita Kiryanov {
152f1a74918SNikita Kiryanov int error = 0;
153f1a74918SNikita Kiryanov u8 ids_buf = 0x00;
154f1a74918SNikita Kiryanov u16 dummy_buf = 0x00;
155f1a74918SNikita Kiryanov u16 cmd = 0x04;
156f1a74918SNikita Kiryanov
157f1a74918SNikita Kiryanov error = spi_set_wordlen(spi, 9);
158f1a74918SNikita Kiryanov if (error)
159f1a74918SNikita Kiryanov return error;
160f1a74918SNikita Kiryanov
161f1a74918SNikita Kiryanov /* Here 9 bits required to transmit a command */
162f1a74918SNikita Kiryanov error = spi_xfer(spi, 9, &cmd, NULL, SPI_XFER_ONCE);
163f1a74918SNikita Kiryanov if (error)
164f1a74918SNikita Kiryanov return error;
165f1a74918SNikita Kiryanov
166f1a74918SNikita Kiryanov /*
167f1a74918SNikita Kiryanov * Here 8 + 1 bits required to arrange extra clock cycle
168f1a74918SNikita Kiryanov * before the first data bit.
169f1a74918SNikita Kiryanov * According to the datasheet - first parameter is the dummy data.
170f1a74918SNikita Kiryanov */
171f1a74918SNikita Kiryanov error = spi_xfer(spi, 9, NULL, &dummy_buf, SPI_XFER_ONCE);
172f1a74918SNikita Kiryanov if (error)
173f1a74918SNikita Kiryanov return error;
174f1a74918SNikita Kiryanov
175f1a74918SNikita Kiryanov error = spi_set_wordlen(spi, 8);
176f1a74918SNikita Kiryanov if (error)
177f1a74918SNikita Kiryanov return error;
178f1a74918SNikita Kiryanov
179f1a74918SNikita Kiryanov /* Read rest of the data */
180f1a74918SNikita Kiryanov error = spi_xfer(spi, 8, NULL, &ids_buf, SPI_XFER_ONCE);
181f1a74918SNikita Kiryanov if (error)
182f1a74918SNikita Kiryanov return error;
183f1a74918SNikita Kiryanov
184f1a74918SNikita Kiryanov *rddid = ids_buf;
185f1a74918SNikita Kiryanov
186f1a74918SNikita Kiryanov return 0;
187f1a74918SNikita Kiryanov }
188f1a74918SNikita Kiryanov
scf0403_spi_transfer(struct spi_slave * spi,struct scf0403_cmd * cmd)189f1a74918SNikita Kiryanov static int scf0403_spi_transfer(struct spi_slave *spi, struct scf0403_cmd *cmd)
190f1a74918SNikita Kiryanov {
191f1a74918SNikita Kiryanov int i, error;
192f1a74918SNikita Kiryanov u32 command = cmd->cmd;
193f1a74918SNikita Kiryanov u32 msg;
194f1a74918SNikita Kiryanov
195f1a74918SNikita Kiryanov error = spi_set_wordlen(spi, 9);
196f1a74918SNikita Kiryanov if (error)
197f1a74918SNikita Kiryanov return error;
198f1a74918SNikita Kiryanov
199f1a74918SNikita Kiryanov error = spi_xfer(spi, 9, &command, NULL, SPI_XFER_ONCE);
200f1a74918SNikita Kiryanov if (error)
201f1a74918SNikita Kiryanov return error;
202f1a74918SNikita Kiryanov
203f1a74918SNikita Kiryanov for (i = 0; i < cmd->count; i++) {
204f1a74918SNikita Kiryanov msg = (cmd->params[i] | 0x100);
205f1a74918SNikita Kiryanov error = spi_xfer(spi, 9, &msg, NULL, SPI_XFER_ONCE);
206f1a74918SNikita Kiryanov if (error)
207f1a74918SNikita Kiryanov return error;
208f1a74918SNikita Kiryanov }
209f1a74918SNikita Kiryanov
210f1a74918SNikita Kiryanov return 0;
211f1a74918SNikita Kiryanov }
212f1a74918SNikita Kiryanov
scf0403_lcd_init(struct scf0403_priv * priv)213f1a74918SNikita Kiryanov static void scf0403_lcd_init(struct scf0403_priv *priv)
214f1a74918SNikita Kiryanov {
215f1a74918SNikita Kiryanov int i;
216f1a74918SNikita Kiryanov
217f1a74918SNikita Kiryanov /* reset LCD */
218f1a74918SNikita Kiryanov scf0403_gpio_reset(priv->reset_gpio);
219f1a74918SNikita Kiryanov
220f1a74918SNikita Kiryanov for (i = 0; i < priv->seq_size; i++) {
221f1a74918SNikita Kiryanov if (scf0403_spi_transfer(priv->spi, &priv->init_seq[i].cmd) < 0)
222f1a74918SNikita Kiryanov puts("SPI transfer failed\n");
223f1a74918SNikita Kiryanov
224f1a74918SNikita Kiryanov mdelay(priv->init_seq[i].delay_ms);
225f1a74918SNikita Kiryanov }
226f1a74918SNikita Kiryanov }
227f1a74918SNikita Kiryanov
scf0403_request_reset_gpio(unsigned gpio)228f1a74918SNikita Kiryanov static int scf0403_request_reset_gpio(unsigned gpio)
229f1a74918SNikita Kiryanov {
230f1a74918SNikita Kiryanov int err = gpio_request(gpio, "lcd reset");
231f1a74918SNikita Kiryanov
232f1a74918SNikita Kiryanov if (err)
233f1a74918SNikita Kiryanov return err;
234f1a74918SNikita Kiryanov
235f1a74918SNikita Kiryanov err = gpio_direction_output(gpio, 0);
236f1a74918SNikita Kiryanov if (err)
237f1a74918SNikita Kiryanov gpio_free(gpio);
238f1a74918SNikita Kiryanov
239f1a74918SNikita Kiryanov return err;
240f1a74918SNikita Kiryanov }
241f1a74918SNikita Kiryanov
scf0403_init(int reset_gpio)242f1a74918SNikita Kiryanov int scf0403_init(int reset_gpio)
243f1a74918SNikita Kiryanov {
244f1a74918SNikita Kiryanov int error;
245f1a74918SNikita Kiryanov
246f1a74918SNikita Kiryanov if (gpio_is_valid(reset_gpio)) {
247f1a74918SNikita Kiryanov error = scf0403_request_reset_gpio(reset_gpio);
248f1a74918SNikita Kiryanov if (error) {
249f1a74918SNikita Kiryanov printf("Failed requesting reset GPIO%d: %d\n",
250f1a74918SNikita Kiryanov reset_gpio, error);
251f1a74918SNikita Kiryanov return error;
252f1a74918SNikita Kiryanov }
253f1a74918SNikita Kiryanov }
254f1a74918SNikita Kiryanov
255f1a74918SNikita Kiryanov priv.reset_gpio = reset_gpio;
256f1a74918SNikita Kiryanov priv.spi = spi_setup_slave(3, 0, 1000000, SPI_MODE_0);
257f1a74918SNikita Kiryanov error = spi_claim_bus(priv.spi);
258f1a74918SNikita Kiryanov if (error)
259f1a74918SNikita Kiryanov goto bus_claim_fail;
260f1a74918SNikita Kiryanov
261f1a74918SNikita Kiryanov /* reset LCD */
262f1a74918SNikita Kiryanov scf0403_gpio_reset(reset_gpio);
263f1a74918SNikita Kiryanov
264f1a74918SNikita Kiryanov error = scf0403_spi_read_rddid(priv.spi, &priv.rddid);
265f1a74918SNikita Kiryanov if (error) {
266f1a74918SNikita Kiryanov puts("IDs read failed\n");
267f1a74918SNikita Kiryanov goto readid_fail;
268f1a74918SNikita Kiryanov }
269f1a74918SNikita Kiryanov
270f1a74918SNikita Kiryanov if (priv.rddid == SCF0403852GGU04_ID) {
271f1a74918SNikita Kiryanov priv.init_seq = scf0403_initseq_sn04;
272f1a74918SNikita Kiryanov priv.seq_size = ARRAY_SIZE(scf0403_initseq_sn04);
273f1a74918SNikita Kiryanov } else {
274f1a74918SNikita Kiryanov priv.init_seq = scf0403_initseq_sn20;
275f1a74918SNikita Kiryanov priv.seq_size = ARRAY_SIZE(scf0403_initseq_sn20);
276f1a74918SNikita Kiryanov }
277f1a74918SNikita Kiryanov
278f1a74918SNikita Kiryanov scf0403_lcd_init(&priv);
279f1a74918SNikita Kiryanov
280f1a74918SNikita Kiryanov /* Start operation */
281f1a74918SNikita Kiryanov scf0403_spi_transfer(priv.spi, &scf0403_cmd_dison);
282f1a74918SNikita Kiryanov mdelay(100);
283f1a74918SNikita Kiryanov scf0403_spi_transfer(priv.spi, &scf0403_cmd_slpout);
284f1a74918SNikita Kiryanov spi_release_bus(priv.spi);
285f1a74918SNikita Kiryanov
286f1a74918SNikita Kiryanov return 0;
287f1a74918SNikita Kiryanov
288f1a74918SNikita Kiryanov readid_fail:
289f1a74918SNikita Kiryanov spi_release_bus(priv.spi);
290f1a74918SNikita Kiryanov bus_claim_fail:
291f1a74918SNikita Kiryanov if (gpio_is_valid(priv.reset_gpio))
292f1a74918SNikita Kiryanov gpio_free(priv.reset_gpio);
293f1a74918SNikita Kiryanov
294f1a74918SNikita Kiryanov return error;
295f1a74918SNikita Kiryanov }
296