1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Raydium RM67191 MIPI-DSI panel driver
4  *
5  * Copyright 2019 NXP
6  */
7 
8 #include <linux/backlight.h>
9 #include <linux/delay.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/regulator/consumer.h>
14 
15 #include <video/mipi_display.h>
16 #include <video/of_videomode.h>
17 #include <video/videomode.h>
18 
19 #include <drm/drm_crtc.h>
20 #include <drm/drm_mipi_dsi.h>
21 #include <drm/drm_panel.h>
22 
23 /* Panel specific color-format bits */
24 #define COL_FMT_16BPP 0x55
25 #define COL_FMT_18BPP 0x66
26 #define COL_FMT_24BPP 0x77
27 
28 /* Write Manufacture Command Set Control */
29 #define WRMAUCCTR 0xFE
30 
31 /* Manufacturer Command Set pages (CMD2) */
32 struct cmd_set_entry {
33 	u8 cmd;
34 	u8 param;
35 };
36 
37 /*
38  * There is no description in the Reference Manual about these commands.
39  * We received them from vendor, so just use them as is.
40  */
41 static const struct cmd_set_entry manufacturer_cmd_set[] = {
42 	{0xFE, 0x0B},
43 	{0x28, 0x40},
44 	{0x29, 0x4F},
45 	{0xFE, 0x0E},
46 	{0x4B, 0x00},
47 	{0x4C, 0x0F},
48 	{0x4D, 0x20},
49 	{0x4E, 0x40},
50 	{0x4F, 0x60},
51 	{0x50, 0xA0},
52 	{0x51, 0xC0},
53 	{0x52, 0xE0},
54 	{0x53, 0xFF},
55 	{0xFE, 0x0D},
56 	{0x18, 0x08},
57 	{0x42, 0x00},
58 	{0x08, 0x41},
59 	{0x46, 0x02},
60 	{0x72, 0x09},
61 	{0xFE, 0x0A},
62 	{0x24, 0x17},
63 	{0x04, 0x07},
64 	{0x1A, 0x0C},
65 	{0x0F, 0x44},
66 	{0xFE, 0x04},
67 	{0x00, 0x0C},
68 	{0x05, 0x08},
69 	{0x06, 0x08},
70 	{0x08, 0x08},
71 	{0x09, 0x08},
72 	{0x0A, 0xE6},
73 	{0x0B, 0x8C},
74 	{0x1A, 0x12},
75 	{0x1E, 0xE0},
76 	{0x29, 0x93},
77 	{0x2A, 0x93},
78 	{0x2F, 0x02},
79 	{0x31, 0x02},
80 	{0x33, 0x05},
81 	{0x37, 0x2D},
82 	{0x38, 0x2D},
83 	{0x3A, 0x1E},
84 	{0x3B, 0x1E},
85 	{0x3D, 0x27},
86 	{0x3F, 0x80},
87 	{0x40, 0x40},
88 	{0x41, 0xE0},
89 	{0x4F, 0x2F},
90 	{0x50, 0x1E},
91 	{0xFE, 0x06},
92 	{0x00, 0xCC},
93 	{0x05, 0x05},
94 	{0x07, 0xA2},
95 	{0x08, 0xCC},
96 	{0x0D, 0x03},
97 	{0x0F, 0xA2},
98 	{0x32, 0xCC},
99 	{0x37, 0x05},
100 	{0x39, 0x83},
101 	{0x3A, 0xCC},
102 	{0x41, 0x04},
103 	{0x43, 0x83},
104 	{0x44, 0xCC},
105 	{0x49, 0x05},
106 	{0x4B, 0xA2},
107 	{0x4C, 0xCC},
108 	{0x51, 0x03},
109 	{0x53, 0xA2},
110 	{0x75, 0xCC},
111 	{0x7A, 0x03},
112 	{0x7C, 0x83},
113 	{0x7D, 0xCC},
114 	{0x82, 0x02},
115 	{0x84, 0x83},
116 	{0x85, 0xEC},
117 	{0x86, 0x0F},
118 	{0x87, 0xFF},
119 	{0x88, 0x00},
120 	{0x8A, 0x02},
121 	{0x8C, 0xA2},
122 	{0x8D, 0xEA},
123 	{0x8E, 0x01},
124 	{0x8F, 0xE8},
125 	{0xFE, 0x06},
126 	{0x90, 0x0A},
127 	{0x92, 0x06},
128 	{0x93, 0xA0},
129 	{0x94, 0xA8},
130 	{0x95, 0xEC},
131 	{0x96, 0x0F},
132 	{0x97, 0xFF},
133 	{0x98, 0x00},
134 	{0x9A, 0x02},
135 	{0x9C, 0xA2},
136 	{0xAC, 0x04},
137 	{0xFE, 0x06},
138 	{0xB1, 0x12},
139 	{0xB2, 0x17},
140 	{0xB3, 0x17},
141 	{0xB4, 0x17},
142 	{0xB5, 0x17},
143 	{0xB6, 0x11},
144 	{0xB7, 0x08},
145 	{0xB8, 0x09},
146 	{0xB9, 0x06},
147 	{0xBA, 0x07},
148 	{0xBB, 0x17},
149 	{0xBC, 0x17},
150 	{0xBD, 0x17},
151 	{0xBE, 0x17},
152 	{0xBF, 0x17},
153 	{0xC0, 0x17},
154 	{0xC1, 0x17},
155 	{0xC2, 0x17},
156 	{0xC3, 0x17},
157 	{0xC4, 0x0F},
158 	{0xC5, 0x0E},
159 	{0xC6, 0x00},
160 	{0xC7, 0x01},
161 	{0xC8, 0x10},
162 	{0xFE, 0x06},
163 	{0x95, 0xEC},
164 	{0x8D, 0xEE},
165 	{0x44, 0xEC},
166 	{0x4C, 0xEC},
167 	{0x32, 0xEC},
168 	{0x3A, 0xEC},
169 	{0x7D, 0xEC},
170 	{0x75, 0xEC},
171 	{0x00, 0xEC},
172 	{0x08, 0xEC},
173 	{0x85, 0xEC},
174 	{0xA6, 0x21},
175 	{0xA7, 0x05},
176 	{0xA9, 0x06},
177 	{0x82, 0x06},
178 	{0x41, 0x06},
179 	{0x7A, 0x07},
180 	{0x37, 0x07},
181 	{0x05, 0x06},
182 	{0x49, 0x06},
183 	{0x0D, 0x04},
184 	{0x51, 0x04},
185 };
186 
187 static const u32 rad_bus_formats[] = {
188 	MEDIA_BUS_FMT_RGB888_1X24,
189 	MEDIA_BUS_FMT_RGB666_1X18,
190 	MEDIA_BUS_FMT_RGB565_1X16,
191 };
192 
193 static const u32 rad_bus_flags = DRM_BUS_FLAG_DE_LOW |
194 				 DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE;
195 
196 struct rad_panel {
197 	struct drm_panel panel;
198 	struct mipi_dsi_device *dsi;
199 
200 	struct gpio_desc *reset;
201 	struct backlight_device *backlight;
202 
203 	struct regulator_bulk_data *supplies;
204 	unsigned int num_supplies;
205 
206 	bool prepared;
207 	bool enabled;
208 };
209 
210 static const struct drm_display_mode default_mode = {
211 	.clock = 132000,
212 	.hdisplay = 1080,
213 	.hsync_start = 1080 + 20,
214 	.hsync_end = 1080 + 20 + 2,
215 	.htotal = 1080 + 20 + 2 + 34,
216 	.vdisplay = 1920,
217 	.vsync_start = 1920 + 10,
218 	.vsync_end = 1920 + 10 + 2,
219 	.vtotal = 1920 + 10 + 2 + 4,
220 	.width_mm = 68,
221 	.height_mm = 121,
222 	.flags = DRM_MODE_FLAG_NHSYNC |
223 		 DRM_MODE_FLAG_NVSYNC,
224 };
225 
226 static inline struct rad_panel *to_rad_panel(struct drm_panel *panel)
227 {
228 	return container_of(panel, struct rad_panel, panel);
229 }
230 
231 static int rad_panel_push_cmd_list(struct mipi_dsi_device *dsi)
232 {
233 	size_t i;
234 	size_t count = ARRAY_SIZE(manufacturer_cmd_set);
235 	int ret = 0;
236 
237 	for (i = 0; i < count; i++) {
238 		const struct cmd_set_entry *entry = &manufacturer_cmd_set[i];
239 		u8 buffer[2] = { entry->cmd, entry->param };
240 
241 		ret = mipi_dsi_generic_write(dsi, &buffer, sizeof(buffer));
242 		if (ret < 0)
243 			return ret;
244 	}
245 
246 	return ret;
247 };
248 
249 static int color_format_from_dsi_format(enum mipi_dsi_pixel_format format)
250 {
251 	switch (format) {
252 	case MIPI_DSI_FMT_RGB565:
253 		return COL_FMT_16BPP;
254 	case MIPI_DSI_FMT_RGB666:
255 	case MIPI_DSI_FMT_RGB666_PACKED:
256 		return COL_FMT_18BPP;
257 	case MIPI_DSI_FMT_RGB888:
258 		return COL_FMT_24BPP;
259 	default:
260 		return COL_FMT_24BPP; /* for backward compatibility */
261 	}
262 };
263 
264 static int rad_panel_prepare(struct drm_panel *panel)
265 {
266 	struct rad_panel *rad = to_rad_panel(panel);
267 	int ret;
268 
269 	if (rad->prepared)
270 		return 0;
271 
272 	ret = regulator_bulk_enable(rad->num_supplies, rad->supplies);
273 	if (ret)
274 		return ret;
275 
276 	if (rad->reset) {
277 		gpiod_set_value_cansleep(rad->reset, 1);
278 		usleep_range(3000, 5000);
279 		gpiod_set_value_cansleep(rad->reset, 0);
280 		usleep_range(18000, 20000);
281 	}
282 
283 	rad->prepared = true;
284 
285 	return 0;
286 }
287 
288 static int rad_panel_unprepare(struct drm_panel *panel)
289 {
290 	struct rad_panel *rad = to_rad_panel(panel);
291 	int ret;
292 
293 	if (!rad->prepared)
294 		return 0;
295 
296 	/*
297 	 * Right after asserting the reset, we need to release it, so that the
298 	 * touch driver can have an active connection with the touch controller
299 	 * even after the display is turned off.
300 	 */
301 	if (rad->reset) {
302 		gpiod_set_value_cansleep(rad->reset, 1);
303 		usleep_range(15000, 17000);
304 		gpiod_set_value_cansleep(rad->reset, 0);
305 	}
306 
307 	ret = regulator_bulk_disable(rad->num_supplies, rad->supplies);
308 	if (ret)
309 		return ret;
310 
311 	rad->prepared = false;
312 
313 	return 0;
314 }
315 
316 static int rad_panel_enable(struct drm_panel *panel)
317 {
318 	struct rad_panel *rad = to_rad_panel(panel);
319 	struct mipi_dsi_device *dsi = rad->dsi;
320 	struct device *dev = &dsi->dev;
321 	int color_format = color_format_from_dsi_format(dsi->format);
322 	int ret;
323 
324 	if (rad->enabled)
325 		return 0;
326 
327 	dsi->mode_flags |= MIPI_DSI_MODE_LPM;
328 
329 	ret = rad_panel_push_cmd_list(dsi);
330 	if (ret < 0) {
331 		dev_err(dev, "Failed to send MCS (%d)\n", ret);
332 		goto fail;
333 	}
334 
335 	/* Select User Command Set table (CMD1) */
336 	ret = mipi_dsi_generic_write(dsi, (u8[]){ WRMAUCCTR, 0x00 }, 2);
337 	if (ret < 0)
338 		goto fail;
339 
340 	/* Software reset */
341 	ret = mipi_dsi_dcs_soft_reset(dsi);
342 	if (ret < 0) {
343 		dev_err(dev, "Failed to do Software Reset (%d)\n", ret);
344 		goto fail;
345 	}
346 
347 	usleep_range(15000, 17000);
348 
349 	/* Set DSI mode */
350 	ret = mipi_dsi_generic_write(dsi, (u8[]){ 0xC2, 0x0B }, 2);
351 	if (ret < 0) {
352 		dev_err(dev, "Failed to set DSI mode (%d)\n", ret);
353 		goto fail;
354 	}
355 	/* Set tear ON */
356 	ret = mipi_dsi_dcs_set_tear_on(dsi, MIPI_DSI_DCS_TEAR_MODE_VBLANK);
357 	if (ret < 0) {
358 		dev_err(dev, "Failed to set tear ON (%d)\n", ret);
359 		goto fail;
360 	}
361 	/* Set tear scanline */
362 	ret = mipi_dsi_dcs_set_tear_scanline(dsi, 0x380);
363 	if (ret < 0) {
364 		dev_err(dev, "Failed to set tear scanline (%d)\n", ret);
365 		goto fail;
366 	}
367 	/* Set pixel format */
368 	ret = mipi_dsi_dcs_set_pixel_format(dsi, color_format);
369 	dev_dbg(dev, "Interface color format set to 0x%x\n", color_format);
370 	if (ret < 0) {
371 		dev_err(dev, "Failed to set pixel format (%d)\n", ret);
372 		goto fail;
373 	}
374 	/* Exit sleep mode */
375 	ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
376 	if (ret < 0) {
377 		dev_err(dev, "Failed to exit sleep mode (%d)\n", ret);
378 		goto fail;
379 	}
380 
381 	usleep_range(5000, 7000);
382 
383 	ret = mipi_dsi_dcs_set_display_on(dsi);
384 	if (ret < 0) {
385 		dev_err(dev, "Failed to set display ON (%d)\n", ret);
386 		goto fail;
387 	}
388 
389 	backlight_enable(rad->backlight);
390 
391 	rad->enabled = true;
392 
393 	return 0;
394 
395 fail:
396 	gpiod_set_value_cansleep(rad->reset, 1);
397 
398 	return ret;
399 }
400 
401 static int rad_panel_disable(struct drm_panel *panel)
402 {
403 	struct rad_panel *rad = to_rad_panel(panel);
404 	struct mipi_dsi_device *dsi = rad->dsi;
405 	struct device *dev = &dsi->dev;
406 	int ret;
407 
408 	if (!rad->enabled)
409 		return 0;
410 
411 	dsi->mode_flags |= MIPI_DSI_MODE_LPM;
412 
413 	backlight_disable(rad->backlight);
414 
415 	usleep_range(10000, 12000);
416 
417 	ret = mipi_dsi_dcs_set_display_off(dsi);
418 	if (ret < 0) {
419 		dev_err(dev, "Failed to set display OFF (%d)\n", ret);
420 		return ret;
421 	}
422 
423 	usleep_range(5000, 10000);
424 
425 	ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
426 	if (ret < 0) {
427 		dev_err(dev, "Failed to enter sleep mode (%d)\n", ret);
428 		return ret;
429 	}
430 
431 	rad->enabled = false;
432 
433 	return 0;
434 }
435 
436 static int rad_panel_get_modes(struct drm_panel *panel,
437 			       struct drm_connector *connector)
438 {
439 	struct drm_display_mode *mode;
440 
441 	mode = drm_mode_duplicate(connector->dev, &default_mode);
442 	if (!mode) {
443 		dev_err(panel->dev, "failed to add mode %ux%u@%u\n",
444 			default_mode.hdisplay, default_mode.vdisplay,
445 			drm_mode_vrefresh(&default_mode));
446 		return -ENOMEM;
447 	}
448 
449 	drm_mode_set_name(mode);
450 	mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
451 	drm_mode_probed_add(connector, mode);
452 
453 	connector->display_info.width_mm = mode->width_mm;
454 	connector->display_info.height_mm = mode->height_mm;
455 	connector->display_info.bus_flags = rad_bus_flags;
456 
457 	drm_display_info_set_bus_formats(&connector->display_info,
458 					 rad_bus_formats,
459 					 ARRAY_SIZE(rad_bus_formats));
460 	return 1;
461 }
462 
463 static int rad_bl_get_brightness(struct backlight_device *bl)
464 {
465 	struct mipi_dsi_device *dsi = bl_get_data(bl);
466 	struct rad_panel *rad = mipi_dsi_get_drvdata(dsi);
467 	u16 brightness;
468 	int ret;
469 
470 	if (!rad->prepared)
471 		return 0;
472 
473 	dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
474 
475 	ret = mipi_dsi_dcs_get_display_brightness(dsi, &brightness);
476 	if (ret < 0)
477 		return ret;
478 
479 	bl->props.brightness = brightness;
480 
481 	return brightness & 0xff;
482 }
483 
484 static int rad_bl_update_status(struct backlight_device *bl)
485 {
486 	struct mipi_dsi_device *dsi = bl_get_data(bl);
487 	struct rad_panel *rad = mipi_dsi_get_drvdata(dsi);
488 	int ret = 0;
489 
490 	if (!rad->prepared)
491 		return 0;
492 
493 	dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
494 
495 	ret = mipi_dsi_dcs_set_display_brightness(dsi, bl->props.brightness);
496 	if (ret < 0)
497 		return ret;
498 
499 	return 0;
500 }
501 
502 static const struct backlight_ops rad_bl_ops = {
503 	.update_status = rad_bl_update_status,
504 	.get_brightness = rad_bl_get_brightness,
505 };
506 
507 static const struct drm_panel_funcs rad_panel_funcs = {
508 	.prepare = rad_panel_prepare,
509 	.unprepare = rad_panel_unprepare,
510 	.enable = rad_panel_enable,
511 	.disable = rad_panel_disable,
512 	.get_modes = rad_panel_get_modes,
513 };
514 
515 static const char * const rad_supply_names[] = {
516 	"v3p3",
517 	"v1p8",
518 };
519 
520 static int rad_init_regulators(struct rad_panel *rad)
521 {
522 	struct device *dev = &rad->dsi->dev;
523 	int i;
524 
525 	rad->num_supplies = ARRAY_SIZE(rad_supply_names);
526 	rad->supplies = devm_kcalloc(dev, rad->num_supplies,
527 				     sizeof(*rad->supplies), GFP_KERNEL);
528 	if (!rad->supplies)
529 		return -ENOMEM;
530 
531 	for (i = 0; i < rad->num_supplies; i++)
532 		rad->supplies[i].supply = rad_supply_names[i];
533 
534 	return devm_regulator_bulk_get(dev, rad->num_supplies, rad->supplies);
535 };
536 
537 static int rad_panel_probe(struct mipi_dsi_device *dsi)
538 {
539 	struct device *dev = &dsi->dev;
540 	struct device_node *np = dev->of_node;
541 	struct rad_panel *panel;
542 	struct backlight_properties bl_props;
543 	int ret;
544 	u32 video_mode;
545 
546 	panel = devm_kzalloc(&dsi->dev, sizeof(*panel), GFP_KERNEL);
547 	if (!panel)
548 		return -ENOMEM;
549 
550 	mipi_dsi_set_drvdata(dsi, panel);
551 
552 	panel->dsi = dsi;
553 
554 	dsi->format = MIPI_DSI_FMT_RGB888;
555 	dsi->mode_flags =  MIPI_DSI_MODE_VIDEO_HSE | MIPI_DSI_MODE_VIDEO;
556 
557 	ret = of_property_read_u32(np, "video-mode", &video_mode);
558 	if (!ret) {
559 		switch (video_mode) {
560 		case 0:
561 			/* burst mode */
562 			dsi->mode_flags |= MIPI_DSI_MODE_VIDEO_BURST;
563 			break;
564 		case 1:
565 			/* non-burst mode with sync event */
566 			break;
567 		case 2:
568 			/* non-burst mode with sync pulse */
569 			dsi->mode_flags |= MIPI_DSI_MODE_VIDEO_SYNC_PULSE;
570 			break;
571 		default:
572 			dev_warn(dev, "invalid video mode %d\n", video_mode);
573 			break;
574 		}
575 	}
576 
577 	ret = of_property_read_u32(np, "dsi-lanes", &dsi->lanes);
578 	if (ret) {
579 		dev_err(dev, "Failed to get dsi-lanes property (%d)\n", ret);
580 		return ret;
581 	}
582 
583 	panel->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
584 	if (IS_ERR(panel->reset))
585 		return PTR_ERR(panel->reset);
586 
587 	memset(&bl_props, 0, sizeof(bl_props));
588 	bl_props.type = BACKLIGHT_RAW;
589 	bl_props.brightness = 255;
590 	bl_props.max_brightness = 255;
591 
592 	panel->backlight = devm_backlight_device_register(dev, dev_name(dev),
593 							  dev, dsi, &rad_bl_ops,
594 							  &bl_props);
595 	if (IS_ERR(panel->backlight)) {
596 		ret = PTR_ERR(panel->backlight);
597 		dev_err(dev, "Failed to register backlight (%d)\n", ret);
598 		return ret;
599 	}
600 
601 	ret = rad_init_regulators(panel);
602 	if (ret)
603 		return ret;
604 
605 	drm_panel_init(&panel->panel, dev, &rad_panel_funcs,
606 		       DRM_MODE_CONNECTOR_DSI);
607 	dev_set_drvdata(dev, panel);
608 
609 	drm_panel_add(&panel->panel);
610 
611 	ret = mipi_dsi_attach(dsi);
612 	if (ret)
613 		drm_panel_remove(&panel->panel);
614 
615 	return ret;
616 }
617 
618 static int rad_panel_remove(struct mipi_dsi_device *dsi)
619 {
620 	struct rad_panel *rad = mipi_dsi_get_drvdata(dsi);
621 	struct device *dev = &dsi->dev;
622 	int ret;
623 
624 	ret = mipi_dsi_detach(dsi);
625 	if (ret)
626 		dev_err(dev, "Failed to detach from host (%d)\n", ret);
627 
628 	drm_panel_remove(&rad->panel);
629 
630 	return 0;
631 }
632 
633 static void rad_panel_shutdown(struct mipi_dsi_device *dsi)
634 {
635 	struct rad_panel *rad = mipi_dsi_get_drvdata(dsi);
636 
637 	rad_panel_disable(&rad->panel);
638 	rad_panel_unprepare(&rad->panel);
639 }
640 
641 static const struct of_device_id rad_of_match[] = {
642 	{ .compatible = "raydium,rm67191", },
643 	{ /* sentinel */ }
644 };
645 MODULE_DEVICE_TABLE(of, rad_of_match);
646 
647 static struct mipi_dsi_driver rad_panel_driver = {
648 	.driver = {
649 		.name = "panel-raydium-rm67191",
650 		.of_match_table = rad_of_match,
651 	},
652 	.probe = rad_panel_probe,
653 	.remove = rad_panel_remove,
654 	.shutdown = rad_panel_shutdown,
655 };
656 module_mipi_dsi_driver(rad_panel_driver);
657 
658 MODULE_AUTHOR("Robert Chiras <robert.chiras@nxp.com>");
659 MODULE_DESCRIPTION("DRM Driver for Raydium RM67191 MIPI DSI panel");
660 MODULE_LICENSE("GPL v2");
661