Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (c) 2017, Fuzhou Rockchip Electronics Co., Ltd
4 */
5
6#include <linux/backlight.h>
7#include <linux/delay.h>
8#include <linux/gpio/consumer.h>
9#include <linux/module.h>
10#include <linux/of.h>
11#include <linux/of_device.h>
12#include <linux/regulator/consumer.h>
13
14#include <video/mipi_display.h>
15
16#include <drm/drm_crtc.h>
17#include <drm/drm_device.h>
18#include <drm/drm_mipi_dsi.h>
19#include <drm/drm_modes.h>
20#include <drm/drm_panel.h>
21#include <drm/drm_print.h>
22
23struct panel_init_cmd {
24 size_t len;
25 const char *data;
26};
27
28#define _INIT_CMD(...) { \
29 .len = sizeof((char[]){__VA_ARGS__}), \
30 .data = (char[]){__VA_ARGS__} }
31
32struct panel_desc {
33 const struct drm_display_mode *mode;
34 unsigned int bpc;
35 struct {
36 unsigned int width;
37 unsigned int height;
38 } size;
39
40 unsigned long flags;
41 enum mipi_dsi_pixel_format format;
42 const struct panel_init_cmd *init_cmds;
43 unsigned int lanes;
44 const char * const *supply_names;
45 unsigned int num_supplies;
46 unsigned int sleep_mode_delay;
47 unsigned int power_down_delay;
48};
49
50struct innolux_panel {
51 struct drm_panel base;
52 struct mipi_dsi_device *link;
53 const struct panel_desc *desc;
54
55 struct backlight_device *backlight;
56 struct regulator_bulk_data *supplies;
57 struct gpio_desc *enable_gpio;
58
59 bool prepared;
60 bool enabled;
61};
62
63static inline struct innolux_panel *to_innolux_panel(struct drm_panel *panel)
64{
65 return container_of(panel, struct innolux_panel, base);
66}
67
68static int innolux_panel_disable(struct drm_panel *panel)
69{
70 struct innolux_panel *innolux = to_innolux_panel(panel);
71
72 if (!innolux->enabled)
73 return 0;
74
75 backlight_disable(innolux->backlight);
76
77 innolux->enabled = false;
78
79 return 0;
80}
81
82static int innolux_panel_unprepare(struct drm_panel *panel)
83{
84 struct innolux_panel *innolux = to_innolux_panel(panel);
85 int err;
86
87 if (!innolux->prepared)
88 return 0;
89
90 err = mipi_dsi_dcs_set_display_off(innolux->link);
91 if (err < 0)
92 DRM_DEV_ERROR(panel->dev, "failed to set display off: %d\n",
93 err);
94
95 err = mipi_dsi_dcs_enter_sleep_mode(innolux->link);
96 if (err < 0) {
97 DRM_DEV_ERROR(panel->dev, "failed to enter sleep mode: %d\n",
98 err);
99 return err;
100 }
101
102 if (innolux->desc->sleep_mode_delay)
103 msleep(innolux->desc->sleep_mode_delay);
104
105 gpiod_set_value_cansleep(innolux->enable_gpio, 0);
106
107 if (innolux->desc->power_down_delay)
108 msleep(innolux->desc->power_down_delay);
109
110 err = regulator_bulk_disable(innolux->desc->num_supplies,
111 innolux->supplies);
112 if (err < 0)
113 return err;
114
115 innolux->prepared = false;
116
117 return 0;
118}
119
120static int innolux_panel_prepare(struct drm_panel *panel)
121{
122 struct innolux_panel *innolux = to_innolux_panel(panel);
123 int err;
124
125 if (innolux->prepared)
126 return 0;
127
128 gpiod_set_value_cansleep(innolux->enable_gpio, 0);
129
130 err = regulator_bulk_enable(innolux->desc->num_supplies,
131 innolux->supplies);
132 if (err < 0)
133 return err;
134
135 /* p079zca: t2 (20ms), p097pfg: t4 (15ms) */
136 usleep_range(20000, 21000);
137
138 gpiod_set_value_cansleep(innolux->enable_gpio, 1);
139
140 /* p079zca: t4, p097pfg: t5 */
141 usleep_range(20000, 21000);
142
143 if (innolux->desc->init_cmds) {
144 const struct panel_init_cmd *cmds =
145 innolux->desc->init_cmds;
146 unsigned int i;
147
148 for (i = 0; cmds[i].len != 0; i++) {
149 const struct panel_init_cmd *cmd = &cmds[i];
150
151 err = mipi_dsi_generic_write(innolux->link, cmd->data,
152 cmd->len);
153 if (err < 0) {
154 dev_err(panel->dev,
155 "failed to write command %u\n", i);
156 goto poweroff;
157 }
158
159 /*
160 * Included by random guessing, because without this
161 * (or at least, some delay), the panel sometimes
162 * didn't appear to pick up the command sequence.
163 */
164 err = mipi_dsi_dcs_nop(innolux->link);
165 if (err < 0) {
166 dev_err(panel->dev,
167 "failed to send DCS nop: %d\n", err);
168 goto poweroff;
169 }
170 }
171 }
172
173 err = mipi_dsi_dcs_exit_sleep_mode(innolux->link);
174 if (err < 0) {
175 DRM_DEV_ERROR(panel->dev, "failed to exit sleep mode: %d\n",
176 err);
177 goto poweroff;
178 }
179
180 /* T6: 120ms - 1000ms*/
181 msleep(120);
182
183 err = mipi_dsi_dcs_set_display_on(innolux->link);
184 if (err < 0) {
185 DRM_DEV_ERROR(panel->dev, "failed to set display on: %d\n",
186 err);
187 goto poweroff;
188 }
189
190 /* T7: 5ms */
191 usleep_range(5000, 6000);
192
193 innolux->prepared = true;
194
195 return 0;
196
197poweroff:
198 gpiod_set_value_cansleep(innolux->enable_gpio, 0);
199 regulator_bulk_disable(innolux->desc->num_supplies, innolux->supplies);
200
201 return err;
202}
203
204static int innolux_panel_enable(struct drm_panel *panel)
205{
206 struct innolux_panel *innolux = to_innolux_panel(panel);
207 int ret;
208
209 if (innolux->enabled)
210 return 0;
211
212 ret = backlight_enable(innolux->backlight);
213 if (ret) {
214 DRM_DEV_ERROR(panel->drm->dev,
215 "Failed to enable backlight %d\n", ret);
216 return ret;
217 }
218
219 innolux->enabled = true;
220
221 return 0;
222}
223
224static const char * const innolux_p079zca_supply_names[] = {
225 "power",
226};
227
228static const struct drm_display_mode innolux_p079zca_mode = {
229 .clock = 56900,
230 .hdisplay = 768,
231 .hsync_start = 768 + 40,
232 .hsync_end = 768 + 40 + 40,
233 .htotal = 768 + 40 + 40 + 40,
234 .vdisplay = 1024,
235 .vsync_start = 1024 + 20,
236 .vsync_end = 1024 + 20 + 4,
237 .vtotal = 1024 + 20 + 4 + 20,
238 .vrefresh = 60,
239};
240
241static const struct panel_desc innolux_p079zca_panel_desc = {
242 .mode = &innolux_p079zca_mode,
243 .bpc = 8,
244 .size = {
245 .width = 120,
246 .height = 160,
247 },
248 .flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
249 MIPI_DSI_MODE_LPM,
250 .format = MIPI_DSI_FMT_RGB888,
251 .lanes = 4,
252 .supply_names = innolux_p079zca_supply_names,
253 .num_supplies = ARRAY_SIZE(innolux_p079zca_supply_names),
254 .power_down_delay = 80, /* T8: 80ms - 1000ms */
255};
256
257static const char * const innolux_p097pfg_supply_names[] = {
258 "avdd",
259 "avee",
260};
261
262static const struct drm_display_mode innolux_p097pfg_mode = {
263 .clock = 229000,
264 .hdisplay = 1536,
265 .hsync_start = 1536 + 100,
266 .hsync_end = 1536 + 100 + 24,
267 .htotal = 1536 + 100 + 24 + 100,
268 .vdisplay = 2048,
269 .vsync_start = 2048 + 100,
270 .vsync_end = 2048 + 100 + 2,
271 .vtotal = 2048 + 100 + 2 + 18,
272 .vrefresh = 60,
273};
274
275/*
276 * Display manufacturer failed to provide init sequencing according to
277 * https://chromium-review.googlesource.com/c/chromiumos/third_party/coreboot/+/892065/
278 * so the init sequence stems from a register dump of a working panel.
279 */
280static const struct panel_init_cmd innolux_p097pfg_init_cmds[] = {
281 /* page 0 */
282 _INIT_CMD(0xF0, 0x55, 0xAA, 0x52, 0x08, 0x00),
283 _INIT_CMD(0xB1, 0xE8, 0x11),
284 _INIT_CMD(0xB2, 0x25, 0x02),
285 _INIT_CMD(0xB5, 0x08, 0x00),
286 _INIT_CMD(0xBC, 0x0F, 0x00),
287 _INIT_CMD(0xB8, 0x03, 0x06, 0x00, 0x00),
288 _INIT_CMD(0xBD, 0x01, 0x90, 0x14, 0x14),
289 _INIT_CMD(0x6F, 0x01),
290 _INIT_CMD(0xC0, 0x03),
291 _INIT_CMD(0x6F, 0x02),
292 _INIT_CMD(0xC1, 0x0D),
293 _INIT_CMD(0xD9, 0x01, 0x09, 0x70),
294 _INIT_CMD(0xC5, 0x12, 0x21, 0x00),
295 _INIT_CMD(0xBB, 0x93, 0x93),
296
297 /* page 1 */
298 _INIT_CMD(0xF0, 0x55, 0xAA, 0x52, 0x08, 0x01),
299 _INIT_CMD(0xB3, 0x3C, 0x3C),
300 _INIT_CMD(0xB4, 0x0F, 0x0F),
301 _INIT_CMD(0xB9, 0x45, 0x45),
302 _INIT_CMD(0xBA, 0x14, 0x14),
303 _INIT_CMD(0xCA, 0x02),
304 _INIT_CMD(0xCE, 0x04),
305 _INIT_CMD(0xC3, 0x9B, 0x9B),
306 _INIT_CMD(0xD8, 0xC0, 0x03),
307 _INIT_CMD(0xBC, 0x82, 0x01),
308 _INIT_CMD(0xBD, 0x9E, 0x01),
309
310 /* page 2 */
311 _INIT_CMD(0xF0, 0x55, 0xAA, 0x52, 0x08, 0x02),
312 _INIT_CMD(0xB0, 0x82),
313 _INIT_CMD(0xD1, 0x00, 0x00, 0x00, 0x3E, 0x00, 0x82, 0x00, 0xA5,
314 0x00, 0xC1, 0x00, 0xEA, 0x01, 0x0D, 0x01, 0x40),
315 _INIT_CMD(0xD2, 0x01, 0x6A, 0x01, 0xA8, 0x01, 0xDC, 0x02, 0x29,
316 0x02, 0x67, 0x02, 0x68, 0x02, 0xA8, 0x02, 0xF0),
317 _INIT_CMD(0xD3, 0x03, 0x19, 0x03, 0x49, 0x03, 0x67, 0x03, 0x8C,
318 0x03, 0xA6, 0x03, 0xC7, 0x03, 0xDE, 0x03, 0xEC),
319 _INIT_CMD(0xD4, 0x03, 0xFF, 0x03, 0xFF),
320 _INIT_CMD(0xE0, 0x00, 0x00, 0x00, 0x86, 0x00, 0xC5, 0x00, 0xE5,
321 0x00, 0xFF, 0x01, 0x26, 0x01, 0x45, 0x01, 0x75),
322 _INIT_CMD(0xE1, 0x01, 0x9C, 0x01, 0xD5, 0x02, 0x05, 0x02, 0x4D,
323 0x02, 0x86, 0x02, 0x87, 0x02, 0xC3, 0x03, 0x03),
324 _INIT_CMD(0xE2, 0x03, 0x2A, 0x03, 0x56, 0x03, 0x72, 0x03, 0x94,
325 0x03, 0xAC, 0x03, 0xCB, 0x03, 0xE0, 0x03, 0xED),
326 _INIT_CMD(0xE3, 0x03, 0xFF, 0x03, 0xFF),
327
328 /* page 3 */
329 _INIT_CMD(0xF0, 0x55, 0xAA, 0x52, 0x08, 0x03),
330 _INIT_CMD(0xB0, 0x00, 0x00, 0x00, 0x00),
331 _INIT_CMD(0xB1, 0x00, 0x00, 0x00, 0x00),
332 _INIT_CMD(0xB2, 0x00, 0x00, 0x06, 0x04, 0x01, 0x40, 0x85),
333 _INIT_CMD(0xB3, 0x10, 0x07, 0xFC, 0x04, 0x01, 0x40, 0x80),
334 _INIT_CMD(0xB6, 0xF0, 0x08, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01,
335 0x40, 0x80),
336 _INIT_CMD(0xBA, 0xC5, 0x07, 0x00, 0x04, 0x11, 0x25, 0x8C),
337 _INIT_CMD(0xBB, 0xC5, 0x07, 0x00, 0x03, 0x11, 0x25, 0x8C),
338 _INIT_CMD(0xC0, 0x00, 0x3C, 0x00, 0x00, 0x00, 0x80, 0x80),
339 _INIT_CMD(0xC1, 0x00, 0x3C, 0x00, 0x00, 0x00, 0x80, 0x80),
340 _INIT_CMD(0xC4, 0x00, 0x00),
341 _INIT_CMD(0xEF, 0x41),
342
343 /* page 4 */
344 _INIT_CMD(0xF0, 0x55, 0xAA, 0x52, 0x08, 0x04),
345 _INIT_CMD(0xEC, 0x4C),
346
347 /* page 5 */
348 _INIT_CMD(0xF0, 0x55, 0xAA, 0x52, 0x08, 0x05),
349 _INIT_CMD(0xB0, 0x13, 0x03, 0x03, 0x01),
350 _INIT_CMD(0xB1, 0x30, 0x00),
351 _INIT_CMD(0xB2, 0x02, 0x02, 0x00),
352 _INIT_CMD(0xB3, 0x82, 0x23, 0x82, 0x9D),
353 _INIT_CMD(0xB4, 0xC5, 0x75, 0x24, 0x57),
354 _INIT_CMD(0xB5, 0x00, 0xD4, 0x72, 0x11, 0x11, 0xAB, 0x0A),
355 _INIT_CMD(0xB6, 0x00, 0x00, 0xD5, 0x72, 0x24, 0x56),
356 _INIT_CMD(0xB7, 0x5C, 0xDC, 0x5C, 0x5C),
357 _INIT_CMD(0xB9, 0x0C, 0x00, 0x00, 0x01, 0x00),
358 _INIT_CMD(0xC0, 0x75, 0x11, 0x11, 0x54, 0x05),
359 _INIT_CMD(0xC6, 0x00, 0x00, 0x00, 0x00),
360 _INIT_CMD(0xD0, 0x00, 0x48, 0x08, 0x00, 0x00),
361 _INIT_CMD(0xD1, 0x00, 0x48, 0x09, 0x00, 0x00),
362
363 /* page 6 */
364 _INIT_CMD(0xF0, 0x55, 0xAA, 0x52, 0x08, 0x06),
365 _INIT_CMD(0xB0, 0x02, 0x32, 0x32, 0x08, 0x2F),
366 _INIT_CMD(0xB1, 0x2E, 0x15, 0x14, 0x13, 0x12),
367 _INIT_CMD(0xB2, 0x11, 0x10, 0x00, 0x3D, 0x3D),
368 _INIT_CMD(0xB3, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D),
369 _INIT_CMD(0xB4, 0x3D, 0x32),
370 _INIT_CMD(0xB5, 0x03, 0x32, 0x32, 0x09, 0x2F),
371 _INIT_CMD(0xB6, 0x2E, 0x1B, 0x1A, 0x19, 0x18),
372 _INIT_CMD(0xB7, 0x17, 0x16, 0x01, 0x3D, 0x3D),
373 _INIT_CMD(0xB8, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D),
374 _INIT_CMD(0xB9, 0x3D, 0x32),
375 _INIT_CMD(0xC0, 0x01, 0x32, 0x32, 0x09, 0x2F),
376 _INIT_CMD(0xC1, 0x2E, 0x1A, 0x1B, 0x16, 0x17),
377 _INIT_CMD(0xC2, 0x18, 0x19, 0x03, 0x3D, 0x3D),
378 _INIT_CMD(0xC3, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D),
379 _INIT_CMD(0xC4, 0x3D, 0x32),
380 _INIT_CMD(0xC5, 0x00, 0x32, 0x32, 0x08, 0x2F),
381 _INIT_CMD(0xC6, 0x2E, 0x14, 0x15, 0x10, 0x11),
382 _INIT_CMD(0xC7, 0x12, 0x13, 0x02, 0x3D, 0x3D),
383 _INIT_CMD(0xC8, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D),
384 _INIT_CMD(0xC9, 0x3D, 0x32),
385
386 {},
387};
388
389static const struct panel_desc innolux_p097pfg_panel_desc = {
390 .mode = &innolux_p097pfg_mode,
391 .bpc = 8,
392 .size = {
393 .width = 147,
394 .height = 196,
395 },
396 .flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
397 MIPI_DSI_MODE_LPM,
398 .format = MIPI_DSI_FMT_RGB888,
399 .init_cmds = innolux_p097pfg_init_cmds,
400 .lanes = 4,
401 .supply_names = innolux_p097pfg_supply_names,
402 .num_supplies = ARRAY_SIZE(innolux_p097pfg_supply_names),
403 .sleep_mode_delay = 100, /* T15 */
404};
405
406static int innolux_panel_get_modes(struct drm_panel *panel)
407{
408 struct innolux_panel *innolux = to_innolux_panel(panel);
409 const struct drm_display_mode *m = innolux->desc->mode;
410 struct drm_display_mode *mode;
411
412 mode = drm_mode_duplicate(panel->drm, m);
413 if (!mode) {
414 DRM_DEV_ERROR(panel->drm->dev, "failed to add mode %ux%ux@%u\n",
415 m->hdisplay, m->vdisplay, m->vrefresh);
416 return -ENOMEM;
417 }
418
419 drm_mode_set_name(mode);
420
421 drm_mode_probed_add(panel->connector, mode);
422
423 panel->connector->display_info.width_mm =
424 innolux->desc->size.width;
425 panel->connector->display_info.height_mm =
426 innolux->desc->size.height;
427 panel->connector->display_info.bpc = innolux->desc->bpc;
428
429 return 1;
430}
431
432static const struct drm_panel_funcs innolux_panel_funcs = {
433 .disable = innolux_panel_disable,
434 .unprepare = innolux_panel_unprepare,
435 .prepare = innolux_panel_prepare,
436 .enable = innolux_panel_enable,
437 .get_modes = innolux_panel_get_modes,
438};
439
440static const struct of_device_id innolux_of_match[] = {
441 { .compatible = "innolux,p079zca",
442 .data = &innolux_p079zca_panel_desc
443 },
444 { .compatible = "innolux,p097pfg",
445 .data = &innolux_p097pfg_panel_desc
446 },
447 { }
448};
449MODULE_DEVICE_TABLE(of, innolux_of_match);
450
451static int innolux_panel_add(struct mipi_dsi_device *dsi,
452 const struct panel_desc *desc)
453{
454 struct innolux_panel *innolux;
455 struct device *dev = &dsi->dev;
456 int err, i;
457
458 innolux = devm_kzalloc(dev, sizeof(*innolux), GFP_KERNEL);
459 if (!innolux)
460 return -ENOMEM;
461
462 innolux->desc = desc;
463
464 innolux->supplies = devm_kcalloc(dev, desc->num_supplies,
465 sizeof(*innolux->supplies),
466 GFP_KERNEL);
467 if (!innolux->supplies)
468 return -ENOMEM;
469
470 for (i = 0; i < desc->num_supplies; i++)
471 innolux->supplies[i].supply = desc->supply_names[i];
472
473 err = devm_regulator_bulk_get(dev, desc->num_supplies,
474 innolux->supplies);
475 if (err < 0)
476 return err;
477
478 innolux->enable_gpio = devm_gpiod_get_optional(dev, "enable",
479 GPIOD_OUT_HIGH);
480 if (IS_ERR(innolux->enable_gpio)) {
481 err = PTR_ERR(innolux->enable_gpio);
482 dev_dbg(dev, "failed to get enable gpio: %d\n", err);
483 innolux->enable_gpio = NULL;
484 }
485
486 innolux->backlight = devm_of_find_backlight(dev);
487 if (IS_ERR(innolux->backlight))
488 return PTR_ERR(innolux->backlight);
489
490 drm_panel_init(&innolux->base);
491 innolux->base.funcs = &innolux_panel_funcs;
492 innolux->base.dev = dev;
493
494 err = drm_panel_add(&innolux->base);
495 if (err < 0)
496 return err;
497
498 mipi_dsi_set_drvdata(dsi, innolux);
499 innolux->link = dsi;
500
501 return 0;
502}
503
504static void innolux_panel_del(struct innolux_panel *innolux)
505{
506 drm_panel_remove(&innolux->base);
507}
508
509static int innolux_panel_probe(struct mipi_dsi_device *dsi)
510{
511 const struct panel_desc *desc;
512 int err;
513
514 desc = of_device_get_match_data(&dsi->dev);
515 dsi->mode_flags = desc->flags;
516 dsi->format = desc->format;
517 dsi->lanes = desc->lanes;
518
519 err = innolux_panel_add(dsi, desc);
520 if (err < 0)
521 return err;
522
523 return mipi_dsi_attach(dsi);
524}
525
526static int innolux_panel_remove(struct mipi_dsi_device *dsi)
527{
528 struct innolux_panel *innolux = mipi_dsi_get_drvdata(dsi);
529 int err;
530
531 err = innolux_panel_unprepare(&innolux->base);
532 if (err < 0)
533 DRM_DEV_ERROR(&dsi->dev, "failed to unprepare panel: %d\n",
534 err);
535
536 err = innolux_panel_disable(&innolux->base);
537 if (err < 0)
538 DRM_DEV_ERROR(&dsi->dev, "failed to disable panel: %d\n", err);
539
540 err = mipi_dsi_detach(dsi);
541 if (err < 0)
542 DRM_DEV_ERROR(&dsi->dev, "failed to detach from DSI host: %d\n",
543 err);
544
545 innolux_panel_del(innolux);
546
547 return 0;
548}
549
550static void innolux_panel_shutdown(struct mipi_dsi_device *dsi)
551{
552 struct innolux_panel *innolux = mipi_dsi_get_drvdata(dsi);
553
554 innolux_panel_unprepare(&innolux->base);
555 innolux_panel_disable(&innolux->base);
556}
557
558static struct mipi_dsi_driver innolux_panel_driver = {
559 .driver = {
560 .name = "panel-innolux-p079zca",
561 .of_match_table = innolux_of_match,
562 },
563 .probe = innolux_panel_probe,
564 .remove = innolux_panel_remove,
565 .shutdown = innolux_panel_shutdown,
566};
567module_mipi_dsi_driver(innolux_panel_driver);
568
569MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
570MODULE_AUTHOR("Lin Huang <hl@rock-chips.com>");
571MODULE_DESCRIPTION("Innolux P079ZCA panel driver");
572MODULE_LICENSE("GPL v2");
1/*
2 * Copyright (c) 2017, Fuzhou Rockchip Electronics Co., Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 */
9
10#include <linux/backlight.h>
11#include <linux/gpio/consumer.h>
12#include <linux/module.h>
13#include <linux/of.h>
14#include <linux/regulator/consumer.h>
15
16#include <drm/drmP.h>
17#include <drm/drm_crtc.h>
18#include <drm/drm_mipi_dsi.h>
19#include <drm/drm_panel.h>
20
21#include <video/mipi_display.h>
22
23struct innolux_panel {
24 struct drm_panel base;
25 struct mipi_dsi_device *link;
26
27 struct backlight_device *backlight;
28 struct regulator *supply;
29 struct gpio_desc *enable_gpio;
30
31 bool prepared;
32 bool enabled;
33};
34
35static inline struct innolux_panel *to_innolux_panel(struct drm_panel *panel)
36{
37 return container_of(panel, struct innolux_panel, base);
38}
39
40static int innolux_panel_disable(struct drm_panel *panel)
41{
42 struct innolux_panel *innolux = to_innolux_panel(panel);
43 int err;
44
45 if (!innolux->enabled)
46 return 0;
47
48 backlight_disable(innolux->backlight);
49
50 err = mipi_dsi_dcs_set_display_off(innolux->link);
51 if (err < 0)
52 DRM_DEV_ERROR(panel->dev, "failed to set display off: %d\n",
53 err);
54
55 innolux->enabled = false;
56
57 return 0;
58}
59
60static int innolux_panel_unprepare(struct drm_panel *panel)
61{
62 struct innolux_panel *innolux = to_innolux_panel(panel);
63 int err;
64
65 if (!innolux->prepared)
66 return 0;
67
68 err = mipi_dsi_dcs_enter_sleep_mode(innolux->link);
69 if (err < 0) {
70 DRM_DEV_ERROR(panel->dev, "failed to enter sleep mode: %d\n",
71 err);
72 return err;
73 }
74
75 gpiod_set_value_cansleep(innolux->enable_gpio, 0);
76
77 /* T8: 80ms - 1000ms */
78 msleep(80);
79
80 err = regulator_disable(innolux->supply);
81 if (err < 0)
82 return err;
83
84 innolux->prepared = false;
85
86 return 0;
87}
88
89static int innolux_panel_prepare(struct drm_panel *panel)
90{
91 struct innolux_panel *innolux = to_innolux_panel(panel);
92 int err, regulator_err;
93
94 if (innolux->prepared)
95 return 0;
96
97 gpiod_set_value_cansleep(innolux->enable_gpio, 0);
98
99 err = regulator_enable(innolux->supply);
100 if (err < 0)
101 return err;
102
103 /* T2: 15ms - 1000ms */
104 usleep_range(15000, 16000);
105
106 gpiod_set_value_cansleep(innolux->enable_gpio, 1);
107
108 /* T4: 15ms - 1000ms */
109 usleep_range(15000, 16000);
110
111 err = mipi_dsi_dcs_exit_sleep_mode(innolux->link);
112 if (err < 0) {
113 DRM_DEV_ERROR(panel->dev, "failed to exit sleep mode: %d\n",
114 err);
115 goto poweroff;
116 }
117
118 /* T6: 120ms - 1000ms*/
119 msleep(120);
120
121 err = mipi_dsi_dcs_set_display_on(innolux->link);
122 if (err < 0) {
123 DRM_DEV_ERROR(panel->dev, "failed to set display on: %d\n",
124 err);
125 goto poweroff;
126 }
127
128 /* T7: 5ms */
129 usleep_range(5000, 6000);
130
131 innolux->prepared = true;
132
133 return 0;
134
135poweroff:
136 regulator_err = regulator_disable(innolux->supply);
137 if (regulator_err)
138 DRM_DEV_ERROR(panel->dev, "failed to disable regulator: %d\n",
139 regulator_err);
140
141 gpiod_set_value_cansleep(innolux->enable_gpio, 0);
142 return err;
143}
144
145static int innolux_panel_enable(struct drm_panel *panel)
146{
147 struct innolux_panel *innolux = to_innolux_panel(panel);
148 int ret;
149
150 if (innolux->enabled)
151 return 0;
152
153 ret = backlight_enable(innolux->backlight);
154 if (ret) {
155 DRM_DEV_ERROR(panel->drm->dev,
156 "Failed to enable backlight %d\n", ret);
157 return ret;
158 }
159
160 innolux->enabled = true;
161
162 return 0;
163}
164
165static const struct drm_display_mode default_mode = {
166 .clock = 56900,
167 .hdisplay = 768,
168 .hsync_start = 768 + 40,
169 .hsync_end = 768 + 40 + 40,
170 .htotal = 768 + 40 + 40 + 40,
171 .vdisplay = 1024,
172 .vsync_start = 1024 + 20,
173 .vsync_end = 1024 + 20 + 4,
174 .vtotal = 1024 + 20 + 4 + 20,
175 .vrefresh = 60,
176};
177
178static int innolux_panel_get_modes(struct drm_panel *panel)
179{
180 struct drm_display_mode *mode;
181
182 mode = drm_mode_duplicate(panel->drm, &default_mode);
183 if (!mode) {
184 DRM_DEV_ERROR(panel->drm->dev, "failed to add mode %ux%ux@%u\n",
185 default_mode.hdisplay, default_mode.vdisplay,
186 default_mode.vrefresh);
187 return -ENOMEM;
188 }
189
190 drm_mode_set_name(mode);
191
192 drm_mode_probed_add(panel->connector, mode);
193
194 panel->connector->display_info.width_mm = 120;
195 panel->connector->display_info.height_mm = 160;
196 panel->connector->display_info.bpc = 8;
197
198 return 1;
199}
200
201static const struct drm_panel_funcs innolux_panel_funcs = {
202 .disable = innolux_panel_disable,
203 .unprepare = innolux_panel_unprepare,
204 .prepare = innolux_panel_prepare,
205 .enable = innolux_panel_enable,
206 .get_modes = innolux_panel_get_modes,
207};
208
209static const struct of_device_id innolux_of_match[] = {
210 { .compatible = "innolux,p079zca", },
211 { }
212};
213MODULE_DEVICE_TABLE(of, innolux_of_match);
214
215static int innolux_panel_add(struct innolux_panel *innolux)
216{
217 struct device *dev = &innolux->link->dev;
218 int err;
219
220 innolux->supply = devm_regulator_get(dev, "power");
221 if (IS_ERR(innolux->supply))
222 return PTR_ERR(innolux->supply);
223
224 innolux->enable_gpio = devm_gpiod_get_optional(dev, "enable",
225 GPIOD_OUT_HIGH);
226 if (IS_ERR(innolux->enable_gpio)) {
227 err = PTR_ERR(innolux->enable_gpio);
228 dev_dbg(dev, "failed to get enable gpio: %d\n", err);
229 innolux->enable_gpio = NULL;
230 }
231
232 innolux->backlight = devm_of_find_backlight(dev);
233
234 if (IS_ERR(innolux->backlight))
235 return PTR_ERR(innolux->backlight);
236
237 drm_panel_init(&innolux->base);
238 innolux->base.funcs = &innolux_panel_funcs;
239 innolux->base.dev = &innolux->link->dev;
240
241 return drm_panel_add(&innolux->base);
242}
243
244static void innolux_panel_del(struct innolux_panel *innolux)
245{
246 if (innolux->base.dev)
247 drm_panel_remove(&innolux->base);
248}
249
250static int innolux_panel_probe(struct mipi_dsi_device *dsi)
251{
252 struct innolux_panel *innolux;
253 int err;
254
255 dsi->lanes = 4;
256 dsi->format = MIPI_DSI_FMT_RGB888;
257 dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
258 MIPI_DSI_MODE_LPM;
259
260 innolux = devm_kzalloc(&dsi->dev, sizeof(*innolux), GFP_KERNEL);
261 if (!innolux)
262 return -ENOMEM;
263
264 mipi_dsi_set_drvdata(dsi, innolux);
265
266 innolux->link = dsi;
267
268 err = innolux_panel_add(innolux);
269 if (err < 0)
270 return err;
271
272 err = mipi_dsi_attach(dsi);
273 return err;
274}
275
276static int innolux_panel_remove(struct mipi_dsi_device *dsi)
277{
278 struct innolux_panel *innolux = mipi_dsi_get_drvdata(dsi);
279 int err;
280
281 err = innolux_panel_unprepare(&innolux->base);
282 if (err < 0)
283 DRM_DEV_ERROR(&dsi->dev, "failed to unprepare panel: %d\n",
284 err);
285
286 err = innolux_panel_disable(&innolux->base);
287 if (err < 0)
288 DRM_DEV_ERROR(&dsi->dev, "failed to disable panel: %d\n", err);
289
290 err = mipi_dsi_detach(dsi);
291 if (err < 0)
292 DRM_DEV_ERROR(&dsi->dev, "failed to detach from DSI host: %d\n",
293 err);
294
295 drm_panel_detach(&innolux->base);
296 innolux_panel_del(innolux);
297
298 return 0;
299}
300
301static void innolux_panel_shutdown(struct mipi_dsi_device *dsi)
302{
303 struct innolux_panel *innolux = mipi_dsi_get_drvdata(dsi);
304
305 innolux_panel_unprepare(&innolux->base);
306 innolux_panel_disable(&innolux->base);
307}
308
309static struct mipi_dsi_driver innolux_panel_driver = {
310 .driver = {
311 .name = "panel-innolux-p079zca",
312 .of_match_table = innolux_of_match,
313 },
314 .probe = innolux_panel_probe,
315 .remove = innolux_panel_remove,
316 .shutdown = innolux_panel_shutdown,
317};
318module_mipi_dsi_driver(innolux_panel_driver);
319
320MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
321MODULE_DESCRIPTION("Innolux P079ZCA panel driver");
322MODULE_LICENSE("GPL v2");