blob: a29e909decc9800cd41c078bb0d7dd548079490f [file] [log] [blame]
Yannick Fertré5b855d42019-10-07 15:29:08 +02001// 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 otm8009a panel driver is inspired from the Linux Kernel driver
8 * drivers/gpu/drm/panel/panel-orisetech-otm8009a.c.
9 */
Yannick Fertré5b855d42019-10-07 15:29:08 +020010#include <backlight.h>
11#include <dm.h>
12#include <mipi_dsi.h>
13#include <panel.h>
14#include <asm/gpio.h>
Simon Glass9bc15642020-02-03 07:36:16 -070015#include <dm/device_compat.h>
Simon Glassdbd79542020-05-10 11:40:11 -060016#include <linux/delay.h>
Yannick Fertré5b855d42019-10-07 15:29:08 +020017#include <power/regulator.h>
18
19#define OTM8009A_BACKLIGHT_DEFAULT 240
20#define OTM8009A_BACKLIGHT_MAX 255
21
22/* Manufacturer Command Set */
23#define MCS_ADRSFT 0x0000 /* Address Shift Function */
24#define MCS_PANSET 0xB3A6 /* Panel Type Setting */
25#define MCS_SD_CTRL 0xC0A2 /* Source Driver Timing Setting */
26#define MCS_P_DRV_M 0xC0B4 /* Panel Driving Mode */
27#define MCS_OSC_ADJ 0xC181 /* Oscillator Adjustment for Idle/Normal mode */
28#define MCS_RGB_VID_SET 0xC1A1 /* RGB Video Mode Setting */
29#define MCS_SD_PCH_CTRL 0xC480 /* Source Driver Precharge Control */
30#define MCS_NO_DOC1 0xC48A /* Command not documented */
31#define MCS_PWR_CTRL1 0xC580 /* Power Control Setting 1 */
32#define MCS_PWR_CTRL2 0xC590 /* Power Control Setting 2 for Normal Mode */
33#define MCS_PWR_CTRL4 0xC5B0 /* Power Control Setting 4 for DC Voltage */
34#define MCS_PANCTRLSET1 0xCB80 /* Panel Control Setting 1 */
35#define MCS_PANCTRLSET2 0xCB90 /* Panel Control Setting 2 */
36#define MCS_PANCTRLSET3 0xCBA0 /* Panel Control Setting 3 */
37#define MCS_PANCTRLSET4 0xCBB0 /* Panel Control Setting 4 */
38#define MCS_PANCTRLSET5 0xCBC0 /* Panel Control Setting 5 */
39#define MCS_PANCTRLSET6 0xCBD0 /* Panel Control Setting 6 */
40#define MCS_PANCTRLSET7 0xCBE0 /* Panel Control Setting 7 */
41#define MCS_PANCTRLSET8 0xCBF0 /* Panel Control Setting 8 */
42#define MCS_PANU2D1 0xCC80 /* Panel U2D Setting 1 */
43#define MCS_PANU2D2 0xCC90 /* Panel U2D Setting 2 */
44#define MCS_PANU2D3 0xCCA0 /* Panel U2D Setting 3 */
45#define MCS_PAND2U1 0xCCB0 /* Panel D2U Setting 1 */
46#define MCS_PAND2U2 0xCCC0 /* Panel D2U Setting 2 */
47#define MCS_PAND2U3 0xCCD0 /* Panel D2U Setting 3 */
48#define MCS_GOAVST 0xCE80 /* GOA VST Setting */
49#define MCS_GOACLKA1 0xCEA0 /* GOA CLKA1 Setting */
50#define MCS_GOACLKA3 0xCEB0 /* GOA CLKA3 Setting */
51#define MCS_GOAECLK 0xCFC0 /* GOA ECLK Setting */
52#define MCS_NO_DOC2 0xCFD0 /* Command not documented */
53#define MCS_GVDDSET 0xD800 /* GVDD/NGVDD */
54#define MCS_VCOMDC 0xD900 /* VCOM Voltage Setting */
55#define MCS_GMCT2_2P 0xE100 /* Gamma Correction 2.2+ Setting */
56#define MCS_GMCT2_2N 0xE200 /* Gamma Correction 2.2- Setting */
57#define MCS_NO_DOC3 0xF5B6 /* Command not documented */
58#define MCS_CMD2_ENA1 0xFF00 /* Enable Access Command2 "CMD2" */
59#define MCS_CMD2_ENA2 0xFF80 /* Enable Access Orise Command2 */
60
61struct otm8009a_panel_priv {
62 struct udevice *reg;
63 struct gpio_desc reset;
Yannick Fertré5b855d42019-10-07 15:29:08 +020064};
65
66static const struct display_timing default_timing = {
67 .pixelclock.typ = 29700000,
68 .hactive.typ = 480,
69 .hfront_porch.typ = 98,
70 .hback_porch.typ = 98,
71 .hsync_len.typ = 32,
72 .vactive.typ = 800,
73 .vfront_porch.typ = 15,
74 .vback_porch.typ = 14,
75 .vsync_len.typ = 10,
76};
77
78static void otm8009a_dcs_write_buf(struct udevice *dev, const void *data,
79 size_t len)
80{
Simon Glassfa20e932020-12-03 16:55:20 -070081 struct mipi_dsi_panel_plat *plat = dev_get_plat(dev);
Yannick Fertré5b855d42019-10-07 15:29:08 +020082 struct mipi_dsi_device *device = plat->device;
83
84 if (mipi_dsi_dcs_write_buffer(device, data, len) < 0)
85 dev_err(dev, "mipi dsi dcs write buffer failed\n");
86}
87
88static void otm8009a_dcs_write_buf_hs(struct udevice *dev, const void *data,
89 size_t len)
90{
Simon Glassfa20e932020-12-03 16:55:20 -070091 struct mipi_dsi_panel_plat *plat = dev_get_plat(dev);
Yannick Fertré5b855d42019-10-07 15:29:08 +020092 struct mipi_dsi_device *device = plat->device;
93
94 /* data will be sent in dsi hs mode (ie. no lpm) */
95 device->mode_flags &= ~MIPI_DSI_MODE_LPM;
96
97 if (mipi_dsi_dcs_write_buffer(device, data, len) < 0)
98 dev_err(dev, "mipi dsi dcs write buffer failed\n");
99
100 /* restore back the dsi lpm mode */
101 device->mode_flags |= MIPI_DSI_MODE_LPM;
102}
103
104#define dcs_write_seq(dev, seq...) \
105({ \
106 static const u8 d[] = { seq }; \
107 otm8009a_dcs_write_buf(dev, d, ARRAY_SIZE(d)); \
108})
109
110#define dcs_write_seq_hs(dev, seq...) \
111({ \
112 static const u8 d[] = { seq }; \
113 otm8009a_dcs_write_buf_hs(dev, d, ARRAY_SIZE(d)); \
114})
115
116#define dcs_write_cmd_at(dev, cmd, seq...) \
117({ \
118 static const u16 c = cmd; \
119 struct udevice *device = dev; \
120 dcs_write_seq(device, MCS_ADRSFT, (c) & 0xFF); \
121 dcs_write_seq(device, (c) >> 8, seq); \
122})
123
124static int otm8009a_init_sequence(struct udevice *dev)
125{
Simon Glassfa20e932020-12-03 16:55:20 -0700126 struct mipi_dsi_panel_plat *plat = dev_get_plat(dev);
Yannick Fertré5b855d42019-10-07 15:29:08 +0200127 struct mipi_dsi_device *device = plat->device;
128 int ret;
129
130 /* Enter CMD2 */
131 dcs_write_cmd_at(dev, MCS_CMD2_ENA1, 0x80, 0x09, 0x01);
132
133 /* Enter Orise Command2 */
134 dcs_write_cmd_at(dev, MCS_CMD2_ENA2, 0x80, 0x09);
135
136 dcs_write_cmd_at(dev, MCS_SD_PCH_CTRL, 0x30);
137 mdelay(10);
138
139 dcs_write_cmd_at(dev, MCS_NO_DOC1, 0x40);
140 mdelay(10);
141
142 dcs_write_cmd_at(dev, MCS_PWR_CTRL4 + 1, 0xA9);
143 dcs_write_cmd_at(dev, MCS_PWR_CTRL2 + 1, 0x34);
144 dcs_write_cmd_at(dev, MCS_P_DRV_M, 0x50);
145 dcs_write_cmd_at(dev, MCS_VCOMDC, 0x4E);
146 dcs_write_cmd_at(dev, MCS_OSC_ADJ, 0x66); /* 65Hz */
147 dcs_write_cmd_at(dev, MCS_PWR_CTRL2 + 2, 0x01);
148 dcs_write_cmd_at(dev, MCS_PWR_CTRL2 + 5, 0x34);
149 dcs_write_cmd_at(dev, MCS_PWR_CTRL2 + 4, 0x33);
150 dcs_write_cmd_at(dev, MCS_GVDDSET, 0x79, 0x79);
151 dcs_write_cmd_at(dev, MCS_SD_CTRL + 1, 0x1B);
152 dcs_write_cmd_at(dev, MCS_PWR_CTRL1 + 2, 0x83);
153 dcs_write_cmd_at(dev, MCS_SD_PCH_CTRL + 1, 0x83);
154 dcs_write_cmd_at(dev, MCS_RGB_VID_SET, 0x0E);
155 dcs_write_cmd_at(dev, MCS_PANSET, 0x00, 0x01);
156
157 dcs_write_cmd_at(dev, MCS_GOAVST, 0x85, 0x01, 0x00, 0x84, 0x01, 0x00);
158 dcs_write_cmd_at(dev, MCS_GOACLKA1, 0x18, 0x04, 0x03, 0x39, 0x00, 0x00,
159 0x00, 0x18, 0x03, 0x03, 0x3A, 0x00, 0x00, 0x00);
160 dcs_write_cmd_at(dev, MCS_GOACLKA3, 0x18, 0x02, 0x03, 0x3B, 0x00, 0x00,
161 0x00, 0x18, 0x01, 0x03, 0x3C, 0x00, 0x00, 0x00);
162 dcs_write_cmd_at(dev, MCS_GOAECLK, 0x01, 0x01, 0x20, 0x20, 0x00, 0x00,
163 0x01, 0x02, 0x00, 0x00);
164
165 dcs_write_cmd_at(dev, MCS_NO_DOC2, 0x00);
166
167 dcs_write_cmd_at(dev, MCS_PANCTRLSET1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
168 dcs_write_cmd_at(dev, MCS_PANCTRLSET2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
169 0, 0, 0, 0, 0);
170 dcs_write_cmd_at(dev, MCS_PANCTRLSET3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171 0, 0, 0, 0, 0);
172 dcs_write_cmd_at(dev, MCS_PANCTRLSET4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
173 dcs_write_cmd_at(dev, MCS_PANCTRLSET5, 0, 4, 4, 4, 4, 4, 0, 0, 0, 0,
174 0, 0, 0, 0, 0);
175 dcs_write_cmd_at(dev, MCS_PANCTRLSET6, 0, 0, 0, 0, 0, 0, 4, 4, 4, 4,
176 4, 0, 0, 0, 0);
177 dcs_write_cmd_at(dev, MCS_PANCTRLSET7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
178 dcs_write_cmd_at(dev, MCS_PANCTRLSET8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
179 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
180
181 dcs_write_cmd_at(dev, MCS_PANU2D1, 0x00, 0x26, 0x09, 0x0B, 0x01, 0x25,
182 0x00, 0x00, 0x00, 0x00);
183 dcs_write_cmd_at(dev, MCS_PANU2D2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
184 0x00, 0x00, 0x00, 0x00, 0x00, 0x26, 0x0A, 0x0C, 0x02);
185 dcs_write_cmd_at(dev, MCS_PANU2D3, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00,
186 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
187 dcs_write_cmd_at(dev, MCS_PAND2U1, 0x00, 0x25, 0x0C, 0x0A, 0x02, 0x26,
188 0x00, 0x00, 0x00, 0x00);
189 dcs_write_cmd_at(dev, MCS_PAND2U2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
190 0x00, 0x00, 0x00, 0x00, 0x00, 0x25, 0x0B, 0x09, 0x01);
191 dcs_write_cmd_at(dev, MCS_PAND2U3, 0x26, 0x00, 0x00, 0x00, 0x00, 0x00,
192 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
193
194 dcs_write_cmd_at(dev, MCS_PWR_CTRL1 + 1, 0x66);
195
196 dcs_write_cmd_at(dev, MCS_NO_DOC3, 0x06);
197
198 dcs_write_cmd_at(dev, MCS_GMCT2_2P, 0x00, 0x09, 0x0F, 0x0E, 0x07, 0x10,
199 0x0B, 0x0A, 0x04, 0x07, 0x0B, 0x08, 0x0F, 0x10, 0x0A,
200 0x01);
201 dcs_write_cmd_at(dev, MCS_GMCT2_2N, 0x00, 0x09, 0x0F, 0x0E, 0x07, 0x10,
202 0x0B, 0x0A, 0x04, 0x07, 0x0B, 0x08, 0x0F, 0x10, 0x0A,
203 0x01);
204
205 /* Exit CMD2 */
206 dcs_write_cmd_at(dev, MCS_CMD2_ENA1, 0xFF, 0xFF, 0xFF);
207
208 ret = mipi_dsi_dcs_nop(device);
209 if (ret)
210 return ret;
211
212 ret = mipi_dsi_dcs_exit_sleep_mode(device);
213 if (ret)
214 return ret;
215
216 /* Wait for sleep out exit */
217 mdelay(120);
218
219 /* Default portrait 480x800 rgb24 */
220 dcs_write_seq(dev, MIPI_DCS_SET_ADDRESS_MODE, 0x00);
221
222 ret = mipi_dsi_dcs_set_column_address(device, 0,
223 default_timing.hactive.typ - 1);
224 if (ret)
225 return ret;
226
227 ret = mipi_dsi_dcs_set_page_address(device, 0,
228 default_timing.vactive.typ - 1);
229 if (ret)
230 return ret;
231
232 /* See otm8009a driver documentation for pixel format descriptions */
233 ret = mipi_dsi_dcs_set_pixel_format(device, MIPI_DCS_PIXEL_FMT_24BIT |
234 MIPI_DCS_PIXEL_FMT_24BIT << 4);
235 if (ret)
236 return ret;
237
238 /* Disable CABC feature */
239 dcs_write_seq(dev, MIPI_DCS_WRITE_POWER_SAVE, 0x00);
240
241 ret = mipi_dsi_dcs_set_display_on(device);
242 if (ret)
243 return ret;
244
245 ret = mipi_dsi_dcs_nop(device);
246 if (ret)
247 return ret;
248
249 /* Send Command GRAM memory write (no parameters) */
250 dcs_write_seq(dev, MIPI_DCS_WRITE_MEMORY_START);
251
252 return 0;
253}
254
255static int otm8009a_panel_enable_backlight(struct udevice *dev)
256{
Simon Glassfa20e932020-12-03 16:55:20 -0700257 struct mipi_dsi_panel_plat *plat = dev_get_plat(dev);
Yannick Fertré5b855d42019-10-07 15:29:08 +0200258 struct mipi_dsi_device *device = plat->device;
259 int ret;
260
261 ret = mipi_dsi_attach(device);
262 if (ret < 0)
263 return ret;
264
265 ret = otm8009a_init_sequence(dev);
266 if (ret)
267 return ret;
268
269 /*
270 * Power on the backlight with the requested brightness
271 * Note We can not use mipi_dsi_dcs_set_display_brightness()
272 * as otm8009a driver support only 8-bit brightness (1 param).
273 */
274 dcs_write_seq(dev, MIPI_DCS_SET_DISPLAY_BRIGHTNESS,
275 OTM8009A_BACKLIGHT_DEFAULT);
276
277 /* Update Brightness Control & Backlight */
278 dcs_write_seq(dev, MIPI_DCS_WRITE_CONTROL_DISPLAY, 0x24);
279
280 /* Update Brightness Control & Backlight */
281 dcs_write_seq_hs(dev, MIPI_DCS_WRITE_CONTROL_DISPLAY);
282
283 /* Need to wait a few time before sending the first image */
284 mdelay(10);
285
286 return 0;
287}
288
289static int otm8009a_panel_get_display_timing(struct udevice *dev,
290 struct display_timing *timings)
291{
Yannick Fertré5b855d42019-10-07 15:29:08 +0200292 memcpy(timings, &default_timing, sizeof(*timings));
293
Yannick Fertré5b855d42019-10-07 15:29:08 +0200294 return 0;
295}
296
Simon Glassaad29ae2020-12-03 16:55:21 -0700297static int otm8009a_panel_of_to_plat(struct udevice *dev)
Yannick Fertré5b855d42019-10-07 15:29:08 +0200298{
299 struct otm8009a_panel_priv *priv = dev_get_priv(dev);
300 int ret;
301
Marek Vasut8da2cb62023-02-28 02:55:20 +0100302 if (CONFIG_IS_ENABLED(DM_REGULATOR)) {
Yannick Fertré5b855d42019-10-07 15:29:08 +0200303 ret = device_get_supply_regulator(dev, "power-supply",
304 &priv->reg);
305 if (ret && ret != -ENOENT) {
306 dev_err(dev, "Warning: cannot get power supply\n");
307 return ret;
308 }
309 }
310
311 ret = gpio_request_by_name(dev, "reset-gpios", 0, &priv->reset,
312 GPIOD_IS_OUT);
313 if (ret) {
314 dev_err(dev, "warning: cannot get reset GPIO\n");
315 if (ret != -ENOENT)
316 return ret;
317 }
318
319 return 0;
320}
321
322static int otm8009a_panel_probe(struct udevice *dev)
323{
324 struct otm8009a_panel_priv *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700325 struct mipi_dsi_panel_plat *plat = dev_get_plat(dev);
Yannick Fertré5b855d42019-10-07 15:29:08 +0200326 int ret;
327
Marek Vasut8da2cb62023-02-28 02:55:20 +0100328 if (CONFIG_IS_ENABLED(DM_REGULATOR) && priv->reg) {
Yannick Fertré5b855d42019-10-07 15:29:08 +0200329 dev_dbg(dev, "enable regulator '%s'\n", priv->reg->name);
330 ret = regulator_set_enable(priv->reg, true);
331 if (ret)
332 return ret;
333 }
334
335 /* reset panel */
336 dm_gpio_set_value(&priv->reset, true);
337 mdelay(1); /* >50us */
338 dm_gpio_set_value(&priv->reset, false);
339 mdelay(10); /* >5ms */
340
Yannick Fertre00340792020-06-24 10:45:42 +0200341 /* fill characteristics of DSI data link */
342 plat->lanes = 2;
343 plat->format = MIPI_DSI_FMT_RGB888;
344 plat->mode_flags = MIPI_DSI_MODE_VIDEO |
Yannick Fertré5b855d42019-10-07 15:29:08 +0200345 MIPI_DSI_MODE_VIDEO_BURST |
346 MIPI_DSI_MODE_LPM;
347
348 return 0;
349}
350
351static const struct panel_ops otm8009a_panel_ops = {
352 .enable_backlight = otm8009a_panel_enable_backlight,
353 .get_display_timing = otm8009a_panel_get_display_timing,
354};
355
356static const struct udevice_id otm8009a_panel_ids[] = {
357 { .compatible = "orisetech,otm8009a" },
358 { }
359};
360
361U_BOOT_DRIVER(otm8009a_panel) = {
362 .name = "otm8009a_panel",
363 .id = UCLASS_PANEL,
364 .of_match = otm8009a_panel_ids,
365 .ops = &otm8009a_panel_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700366 .of_to_plat = otm8009a_panel_of_to_plat,
Yannick Fertré5b855d42019-10-07 15:29:08 +0200367 .probe = otm8009a_panel_probe,
Simon Glass71fa5b42020-12-03 16:55:18 -0700368 .plat_auto = sizeof(struct mipi_dsi_panel_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700369 .priv_auto = sizeof(struct otm8009a_panel_priv),
Yannick Fertré5b855d42019-10-07 15:29:08 +0200370};