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