853dbc52d61268ef0ab6e74a2a690e53cc19fa73
[oweals/u-boot.git] / drivers / video / raydium-rm68200.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2019 STMicroelectronics - All Rights Reserved
4  * Author(s): Yannick Fertre <yannick.fertre@st.com> for STMicroelectronics.
5  *            Philippe Cornu <philippe.cornu@st.com> for STMicroelectronics.
6  *
7  * This rm68200 panel driver is inspired from the Linux Kernel driver
8  * drivers/gpu/drm/panel/panel-raydium-rm68200.c.
9  */
10 #include <common.h>
11 #include <backlight.h>
12 #include <dm.h>
13 #include <mipi_dsi.h>
14 #include <panel.h>
15 #include <asm/gpio.h>
16 #include <dm/device_compat.h>
17 #include <power/regulator.h>
18
19 /*** Manufacturer Command Set ***/
20 #define MCS_CMD_MODE_SW 0xFE /* CMD Mode Switch */
21 #define MCS_CMD1_UCS    0x00 /* User Command Set (UCS = CMD1) */
22 #define MCS_CMD2_P0     0x01 /* Manufacture Command Set Page0 (CMD2 P0) */
23 #define MCS_CMD2_P1     0x02 /* Manufacture Command Set Page1 (CMD2 P1) */
24 #define MCS_CMD2_P2     0x03 /* Manufacture Command Set Page2 (CMD2 P2) */
25 #define MCS_CMD2_P3     0x04 /* Manufacture Command Set Page3 (CMD2 P3) */
26
27 /* CMD2 P0 commands (Display Options and Power) */
28 #define MCS_STBCTR      0x12 /* TE1 Output Setting Zig-Zag Connection */
29 #define MCS_SGOPCTR     0x16 /* Source Bias Current */
30 #define MCS_SDCTR       0x1A /* Source Output Delay Time */
31 #define MCS_INVCTR      0x1B /* Inversion Type */
32 #define MCS_EXT_PWR_IC  0x24 /* External PWR IC Control */
33 #define MCS_SETAVDD     0x27 /* PFM Control for AVDD Output */
34 #define MCS_SETAVEE     0x29 /* PFM Control for AVEE Output */
35 #define MCS_BT2CTR      0x2B /* DDVDL Charge Pump Control */
36 #define MCS_BT3CTR      0x2F /* VGH Charge Pump Control */
37 #define MCS_BT4CTR      0x34 /* VGL Charge Pump Control */
38 #define MCS_VCMCTR      0x46 /* VCOM Output Level Control */
39 #define MCS_SETVGN      0x52 /* VG M/S N Control */
40 #define MCS_SETVGP      0x54 /* VG M/S P Control */
41 #define MCS_SW_CTRL     0x5F /* Interface Control for PFM and MIPI */
42
43 /* CMD2 P2 commands (GOA Timing Control) - no description in datasheet */
44 #define GOA_VSTV1               0x00
45 #define GOA_VSTV2               0x07
46 #define GOA_VCLK1               0x0E
47 #define GOA_VCLK2               0x17
48 #define GOA_VCLK_OPT1           0x20
49 #define GOA_BICLK1              0x2A
50 #define GOA_BICLK2              0x37
51 #define GOA_BICLK3              0x44
52 #define GOA_BICLK4              0x4F
53 #define GOA_BICLK_OPT1          0x5B
54 #define GOA_BICLK_OPT2          0x60
55 #define MCS_GOA_GPO1            0x6D
56 #define MCS_GOA_GPO2            0x71
57 #define MCS_GOA_EQ              0x74
58 #define MCS_GOA_CLK_GALLON      0x7C
59 #define MCS_GOA_FS_SEL0         0x7E
60 #define MCS_GOA_FS_SEL1         0x87
61 #define MCS_GOA_FS_SEL2         0x91
62 #define MCS_GOA_FS_SEL3         0x9B
63 #define MCS_GOA_BS_SEL0         0xAC
64 #define MCS_GOA_BS_SEL1         0xB5
65 #define MCS_GOA_BS_SEL2         0xBF
66 #define MCS_GOA_BS_SEL3         0xC9
67 #define MCS_GOA_BS_SEL4         0xD3
68
69 /* CMD2 P3 commands (Gamma) */
70 #define MCS_GAMMA_VP            0x60 /* Gamma VP1~VP16 */
71 #define MCS_GAMMA_VN            0x70 /* Gamma VN1~VN16 */
72
73 struct rm68200_panel_priv {
74         struct udevice *reg;
75         struct udevice *backlight;
76         struct gpio_desc reset;
77         unsigned int lanes;
78         enum mipi_dsi_pixel_format format;
79         unsigned long mode_flags;
80 };
81
82 static const struct display_timing default_timing = {
83         .pixelclock.typ         = 54000000,
84         .hactive.typ            = 720,
85         .hfront_porch.typ       = 48,
86         .hback_porch.typ        = 48,
87         .hsync_len.typ          = 9,
88         .vactive.typ            = 1280,
89         .vfront_porch.typ       = 12,
90         .vback_porch.typ        = 12,
91         .vsync_len.typ          = 5,
92 };
93
94 static void rm68200_dcs_write_buf(struct udevice *dev, const void *data,
95                                   size_t len)
96 {
97         struct mipi_dsi_panel_plat *plat = dev_get_platdata(dev);
98         struct mipi_dsi_device *device = plat->device;
99         int err;
100
101         err = mipi_dsi_dcs_write_buffer(device, data, len);
102         if (err < 0)
103                 dev_err(dev, "MIPI DSI DCS write buffer failed: %d\n", err);
104 }
105
106 static void rm68200_dcs_write_cmd(struct udevice *dev, u8 cmd, u8 value)
107 {
108         struct mipi_dsi_panel_plat *plat = dev_get_platdata(dev);
109         struct mipi_dsi_device *device = plat->device;
110         int err;
111
112         err = mipi_dsi_dcs_write(device, cmd, &value, 1);
113         if (err < 0)
114                 dev_err(dev, "MIPI DSI DCS write failed: %d\n", err);
115 }
116
117 #define dcs_write_seq(ctx, seq...)                              \
118 ({                                                              \
119         static const u8 d[] = { seq };                          \
120                                                                 \
121         rm68200_dcs_write_buf(ctx, d, ARRAY_SIZE(d));           \
122 })
123
124 /*
125  * This panel is not able to auto-increment all cmd addresses so for some of
126  * them, we need to send them one by one...
127  */
128 #define dcs_write_cmd_seq(ctx, cmd, seq...)                     \
129 ({                                                              \
130         static const u8 d[] = { seq };                          \
131         unsigned int i;                                         \
132                                                                 \
133         for (i = 0; i < ARRAY_SIZE(d) ; i++)                    \
134                 rm68200_dcs_write_cmd(ctx, cmd + i, d[i]);      \
135 })
136
137 static void rm68200_init_sequence(struct udevice *dev)
138 {
139         /* Enter CMD2 with page 0 */
140         dcs_write_seq(dev, MCS_CMD_MODE_SW, MCS_CMD2_P0);
141         dcs_write_cmd_seq(dev, MCS_EXT_PWR_IC, 0xC0, 0x53, 0x00);
142         dcs_write_seq(dev, MCS_BT2CTR, 0xE5);
143         dcs_write_seq(dev, MCS_SETAVDD, 0x0A);
144         dcs_write_seq(dev, MCS_SETAVEE, 0x0A);
145         dcs_write_seq(dev, MCS_SGOPCTR, 0x52);
146         dcs_write_seq(dev, MCS_BT3CTR, 0x53);
147         dcs_write_seq(dev, MCS_BT4CTR, 0x5A);
148         dcs_write_seq(dev, MCS_INVCTR, 0x00);
149         dcs_write_seq(dev, MCS_STBCTR, 0x0A);
150         dcs_write_seq(dev, MCS_SDCTR, 0x06);
151         dcs_write_seq(dev, MCS_VCMCTR, 0x56);
152         dcs_write_seq(dev, MCS_SETVGN, 0xA0, 0x00);
153         dcs_write_seq(dev, MCS_SETVGP, 0xA0, 0x00);
154         dcs_write_seq(dev, MCS_SW_CTRL, 0x11); /* 2 data lanes, see doc */
155
156         dcs_write_seq(dev, MCS_CMD_MODE_SW, MCS_CMD2_P2);
157         dcs_write_seq(dev, GOA_VSTV1, 0x05);
158         dcs_write_seq(dev, 0x02, 0x0B);
159         dcs_write_seq(dev, 0x03, 0x0F);
160         dcs_write_seq(dev, 0x04, 0x7D, 0x00, 0x50);
161         dcs_write_cmd_seq(dev, GOA_VSTV2, 0x05, 0x16, 0x0D, 0x11, 0x7D, 0x00,
162                           0x50);
163         dcs_write_cmd_seq(dev, GOA_VCLK1, 0x07, 0x08, 0x01, 0x02, 0x00, 0x7D,
164                           0x00, 0x85, 0x08);
165         dcs_write_cmd_seq(dev, GOA_VCLK2, 0x03, 0x04, 0x05, 0x06, 0x00, 0x7D,
166                           0x00, 0x85, 0x08);
167         dcs_write_seq(dev, GOA_VCLK_OPT1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
168                       0x00, 0x00, 0x00, 0x00);
169         dcs_write_cmd_seq(dev, GOA_BICLK1, 0x07, 0x08);
170         dcs_write_seq(dev, 0x2D, 0x01);
171         dcs_write_seq(dev, 0x2F, 0x02, 0x00, 0x40, 0x05, 0x08, 0x54, 0x7D,
172                       0x00);
173         dcs_write_cmd_seq(dev, GOA_BICLK2, 0x03, 0x04, 0x05, 0x06, 0x00);
174         dcs_write_seq(dev, 0x3D, 0x40);
175         dcs_write_seq(dev, 0x3F, 0x05, 0x08, 0x54, 0x7D, 0x00);
176         dcs_write_seq(dev, GOA_BICLK3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
177                       0x00, 0x00, 0x00, 0x00, 0x00);
178         dcs_write_seq(dev, GOA_BICLK4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
179                       0x00, 0x00);
180         dcs_write_seq(dev, 0x58, 0x00, 0x00, 0x00);
181         dcs_write_seq(dev, GOA_BICLK_OPT1, 0x00, 0x00, 0x00, 0x00, 0x00);
182         dcs_write_seq(dev, GOA_BICLK_OPT2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
183                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
184         dcs_write_seq(dev, MCS_GOA_GPO1, 0x00, 0x00, 0x00, 0x00);
185         dcs_write_seq(dev, MCS_GOA_GPO2, 0x00, 0x20, 0x00);
186         dcs_write_seq(dev, MCS_GOA_EQ, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
187                       0x00, 0x00);
188         dcs_write_seq(dev, MCS_GOA_CLK_GALLON, 0x00, 0x00);
189         dcs_write_cmd_seq(dev, MCS_GOA_FS_SEL0, 0xBF, 0x02, 0x06, 0x14, 0x10,
190                           0x16, 0x12, 0x08, 0x3F);
191         dcs_write_cmd_seq(dev, MCS_GOA_FS_SEL1, 0x3F, 0x3F, 0x3F, 0x3F, 0x0C,
192                           0x0A, 0x0E, 0x3F, 0x3F, 0x00);
193         dcs_write_cmd_seq(dev, MCS_GOA_FS_SEL2, 0x04, 0x3F, 0x3F, 0x3F, 0x3F,
194                           0x05, 0x01, 0x3F, 0x3F, 0x0F);
195         dcs_write_cmd_seq(dev, MCS_GOA_FS_SEL3, 0x0B, 0x0D, 0x3F, 0x3F, 0x3F,
196                           0x3F);
197         dcs_write_cmd_seq(dev, 0xA2, 0x3F, 0x09, 0x13, 0x17, 0x11, 0x15);
198         dcs_write_cmd_seq(dev, 0xA9, 0x07, 0x03, 0x3F);
199         dcs_write_cmd_seq(dev, MCS_GOA_BS_SEL0, 0x3F, 0x05, 0x01, 0x17, 0x13,
200                           0x15, 0x11, 0x0F, 0x3F);
201         dcs_write_cmd_seq(dev, MCS_GOA_BS_SEL1, 0x3F, 0x3F, 0x3F, 0x3F, 0x0B,
202                           0x0D, 0x09, 0x3F, 0x3F, 0x07);
203         dcs_write_cmd_seq(dev, MCS_GOA_BS_SEL2, 0x03, 0x3F, 0x3F, 0x3F, 0x3F,
204                           0x02, 0x06, 0x3F, 0x3F, 0x08);
205         dcs_write_cmd_seq(dev, MCS_GOA_BS_SEL3, 0x0C, 0x0A, 0x3F, 0x3F, 0x3F,
206                           0x3F, 0x3F, 0x0E, 0x10, 0x14);
207         dcs_write_cmd_seq(dev, MCS_GOA_BS_SEL4, 0x12, 0x16, 0x00, 0x04, 0x3F);
208         dcs_write_seq(dev, 0xDC, 0x02);
209         dcs_write_seq(dev, 0xDE, 0x12);
210
211         dcs_write_seq(dev, MCS_CMD_MODE_SW, 0x0E); /* No documentation */
212         dcs_write_seq(dev, 0x01, 0x75);
213
214         dcs_write_seq(dev, MCS_CMD_MODE_SW, MCS_CMD2_P3);
215         dcs_write_cmd_seq(dev, MCS_GAMMA_VP, 0x00, 0x0C, 0x12, 0x0E, 0x06,
216                           0x12, 0x0E, 0x0B, 0x15, 0x0B, 0x10, 0x07, 0x0F,
217                           0x12, 0x0C, 0x00);
218         dcs_write_cmd_seq(dev, MCS_GAMMA_VN, 0x00, 0x0C, 0x12, 0x0E, 0x06,
219                           0x12, 0x0E, 0x0B, 0x15, 0x0B, 0x10, 0x07, 0x0F,
220                           0x12, 0x0C, 0x00);
221
222         /* Exit CMD2 */
223         dcs_write_seq(dev, MCS_CMD_MODE_SW, MCS_CMD1_UCS);
224 }
225
226 static int rm68200_panel_enable_backlight(struct udevice *dev)
227 {
228         struct mipi_dsi_panel_plat *plat = dev_get_platdata(dev);
229         struct mipi_dsi_device *device = plat->device;
230         struct rm68200_panel_priv *priv = dev_get_priv(dev);
231         int ret;
232
233         ret = mipi_dsi_attach(device);
234         if (ret < 0)
235                 return ret;
236
237         rm68200_init_sequence(dev);
238
239         ret = mipi_dsi_dcs_exit_sleep_mode(device);
240         if (ret)
241                 return ret;
242
243         mdelay(125);
244
245         ret = mipi_dsi_dcs_set_display_on(device);
246         if (ret)
247                 return ret;
248
249         mdelay(20);
250
251         ret = backlight_enable(priv->backlight);
252         if (ret)
253                 return ret;
254
255         return 0;
256 }
257
258 static int rm68200_panel_get_display_timing(struct udevice *dev,
259                                             struct display_timing *timings)
260 {
261         struct mipi_dsi_panel_plat *plat = dev_get_platdata(dev);
262         struct mipi_dsi_device *device = plat->device;
263         struct rm68200_panel_priv *priv = dev_get_priv(dev);
264
265         memcpy(timings, &default_timing, sizeof(*timings));
266
267         /* fill characteristics of DSI data link */
268         device->lanes = priv->lanes;
269         device->format = priv->format;
270         device->mode_flags = priv->mode_flags;
271
272         return 0;
273 }
274
275 static int rm68200_panel_ofdata_to_platdata(struct udevice *dev)
276 {
277         struct rm68200_panel_priv *priv = dev_get_priv(dev);
278         int ret;
279
280         if (IS_ENABLED(CONFIG_DM_REGULATOR)) {
281                 ret =  device_get_supply_regulator(dev, "power-supply",
282                                                    &priv->reg);
283                 if (ret && ret != -ENOENT) {
284                         dev_err(dev, "Warning: cannot get power supply\n");
285                         return ret;
286                 }
287         }
288
289         ret = gpio_request_by_name(dev, "reset-gpios", 0, &priv->reset,
290                                    GPIOD_IS_OUT);
291         if (ret) {
292                 dev_err(dev, "Warning: cannot get reset GPIO\n");
293                 if (ret != -ENOENT)
294                         return ret;
295         }
296
297         ret = uclass_get_device_by_phandle(UCLASS_PANEL_BACKLIGHT, dev,
298                                            "backlight", &priv->backlight);
299         if (ret) {
300                 dev_err(dev, "Cannot get backlight: ret=%d\n", ret);
301                 return ret;
302         }
303
304         return 0;
305 }
306
307 static int rm68200_panel_probe(struct udevice *dev)
308 {
309         struct rm68200_panel_priv *priv = dev_get_priv(dev);
310         int ret;
311
312         if (IS_ENABLED(CONFIG_DM_REGULATOR) && priv->reg) {
313                 ret = regulator_set_enable(priv->reg, true);
314                 if (ret)
315                         return ret;
316         }
317
318         /* reset panel */
319         dm_gpio_set_value(&priv->reset, true);
320         mdelay(1);
321         dm_gpio_set_value(&priv->reset, false);
322         mdelay(10);
323
324         priv->lanes = 2;
325         priv->format = MIPI_DSI_FMT_RGB888;
326         priv->mode_flags = MIPI_DSI_MODE_VIDEO |
327                            MIPI_DSI_MODE_VIDEO_BURST |
328                            MIPI_DSI_MODE_LPM;
329
330         return 0;
331 }
332
333 static const struct panel_ops rm68200_panel_ops = {
334         .enable_backlight = rm68200_panel_enable_backlight,
335         .get_display_timing = rm68200_panel_get_display_timing,
336 };
337
338 static const struct udevice_id rm68200_panel_ids[] = {
339         { .compatible = "raydium,rm68200" },
340         { }
341 };
342
343 U_BOOT_DRIVER(rm68200_panel) = {
344         .name                     = "rm68200_panel",
345         .id                       = UCLASS_PANEL,
346         .of_match                 = rm68200_panel_ids,
347         .ops                      = &rm68200_panel_ops,
348         .ofdata_to_platdata       = rm68200_panel_ofdata_to_platdata,
349         .probe                    = rm68200_panel_probe,
350         .platdata_auto_alloc_size = sizeof(struct mipi_dsi_panel_plat),
351         .priv_auto_alloc_size   = sizeof(struct rm68200_panel_priv),
352 };