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